ASP.NET MVC3 Using Areas - asp.net-mvc-3

I'm having some troubles setting up two Areas in my project, I created both Areas by right clicking my project -> Add -> Area using VS 2010 Professional,
The code it generated in the *AreaRegistration.cs files is exactly as I'd expect it.
Both my areas have multiple controllers.
I've checked my global.asax.cs file and it contains what I'd expect, the following is at the bottom:
// Generated code
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
When I change the default generated routing info I can get it to work for a Single controller:
context.MapRoute(
"CommunityCourses_default",
"CommunityCourses/{controller}/{action}/{id}",
new {controller="NameOfController", action = "Index", id = UrlParameter.Optional }
);
The controller="NameOfController" is what I've tried adding to the generated code, but it only works for a single controller.
Edit: I renamed the namespace of my files to use PROJECTNAME.AREANAME within an area, is this causing issues? I thought it'd simplify my imports in a non-convention-breaking way.
What am I missing? I've noticed a lot of problems about areas on SO and tried to follow the answers without success. This issue has bothered eluded me for the majority of the day, any help would be greatly appreciated!

If you moved files from the Controllers folder or the VIews folder in the root of the project into Controllers or View folders contained in the {AreaName} folder, then all of those files moved need their namespaces changed from {ProjectName}.{*etCetera} to:
{ProjectName}.Areas.{AreaName}.{*etCetera}

Turns out what was wrong was that the order or routing was incorrect, it was processing {controller}/{action}/{id} first which was breaking my areas. Moving the
AreaRegistration.RegisterAllAreas();
to the top of my application_start fixed my problem.
http://haacked.com/archive/2011/04/13/routedebugger-2.aspx Link to the tool I used to find the problem, unfortunately the current version of the tool doesn't work on the default 404 pages, so I also had to create a custom 404 page for it to work.

Related

How to change default view creation logic in visual studio?

I've looked here and here to try to answer this question as well as a billion Google searches, but have yet to come up with a solution.
My directory structure is a multi-tenant MVC site with an overridden view engine that uses Areas to serve content for different hosts that share a lot of common logic served globally from the base controllers and views folders. We have taken this a step further and broken out different global site sections into a new folder called SiteSections. Inside of this folder we have more Areas.
The issue I am having, is whenever I try to use the visual studio context menu from inside a controller that is inside the SiteSections folder, it always adds it to the global Views folder.
What I am assuming is happening is that since these are Areas held within a different directory, Visual Studio is searching the Areas folder for an Area with the name of which I am working in. Since this is kept in a different directory, it is just defaulting to the global one. I've looked into all the different T4 templates and do not see anything specifying the directory where the view will be created.
I have just one question, that I'm hoping is possible.
How can I override Visual Studio to look in a second directory for the Area in question?
Thanks in advance!
I experienced something like that, not with Areas, but with Folders.
Have you tried to add custom view engine on ViewEngines?
The steps that i followed:
1 - I put this line at Global.asax.cs on method Application_Start:
ViewsEngines.Engines.Add(new MyCustomViewEngine());
2 - I created the file named as MyCustomViewEngine inheriting from RazorViewEngine, for example:
public class MyCustomViewEngine : RazorViewEngine
{
public MyCustomViewEngine()
{
base.ViewLocationFormats = MyViewLocationFormats;
}
private static string[] MyViewLocationFormats = new[]
{
"~/Views/Folder/{0}.cshtml",
"~/Views/Folder_1/Folder_2/{0}.cshtml"
}
}
I think that you can use in this way: "~/Areas/Views/Folder/{0}.cshtml",
Hope this helps!

References for MVC extension methods differ between vbhtml and cshtml files?

I am converting the login views of an application from vbhtml to cshtml. In the vbhtml files the original developer calls an Html.LabelWithAttributesFor extension method. When I write the same line of code in the new cshtml file the extension method isn't available via Intellisense and fails to compile. Are references not shared between vbhtml and cshtml files in a project?
Wanted to circle back and close this question with the "answer". In the end, what seems to have knocked something loose was when we removed the vbhtml pages from the solution. All use of the Extensions had been removed prior to our removing the vbhtml pages and other developers had worked on the solution in the interim making other changes. Fast forward a few months to another developer needing the extensions. He added a reference to the project with the extension methods and used them on a new cshtml page, which worked fine. So, the problem is solved but I still have no idea what caused the problem in the first place and I have been unable to reproduce it.

Global.asax.cs is not visible at the server

Running under Server08 | IIS7. I have a website project and am in the habit of hand editing the Global.asax.cs at the deployed site many times in the past. Recently I've found that only the Global.asax is present and has only:
<%# Application Codebehind="Global.asax.cs" Inherits="myDomain.MvcApplication" Language="C#" %>
There are simply no Global.asax.cs files present/visible for any of my active, functioning websites anymore.
I've checked here Global.asax can't find code-behind class and here where is the codes in Global.asax in mvc3? without getting anywhere. The second link contains the comment: 'That's because it's a compiled web application. You'll have to view it in Visual Studio as a project.'
But since my project has always been a website - and I've not converted it (intentionally) I'm puzzled by the changed behavior.
But when i step into VS12 and look at the context menu for the solution's WebProject it presents 'Convert to Web Application'. The sites' folder structures do not contain App_Data or App_Start. To my mind, these 2 fact establish at VS is treating the project as a website, why then, is the Global.cs compiled down to the /bin?
I'll close by repeating - I've hand edited these things in the past - I'm not positive but it's probably accurate to say that this is the first time I've tried to do so after installing VS12. And, coming to think on it harder - it's only recently that I've implemented the 'One Click Publishing' service so that could be coming into play.
verify? I'd like I need to edit a simple update without full re-deployment.
thx
I think the 'One Click Publishing' is most likely the culprit here. I created a sample Web Site project to experiment, and when I use the publish feature in Visual Studio, the files generated are indeed missing the Global.asax.cs file. Instead, a bin folder is created with a compilation called "App_global.asax.dll" even though it is a Web Site and not a Web Application. I am guessing this might be similar to what is happening for you as well.
If not, I found a link which might be useful to you in order to once again be able to edit a class on the server. Particularly, have a look at the second answer (the one not accepted as the best answer) to recreate a class file for the global code: Where is the Global.asax.cs file?
I tried this solution, and verified that the class file is editable on the server, and that it is dynamically compiled at run time (the modifications I made to the file worked immediately).
Hope this helps!
After editing the global.asax you need to rebuild and upload the DLL to the bin folder. Otherwise, your changes will not take effect.
You might be able to upload an web version of the global.asax that includes the code in that one file, which obviously does not require a .cs code behind. I used to do it with aspx files in an application but I have never tried it in the GLobal.asax file.

How to deploy aspdotnetstorefront source code changes?

I'm implementing an AspDotNetStorefront (ASPDNSF) site for a client, they've purchased the source code and I'm just trying to do something simple like add an ID for CSS styling to the product images.
I've gone into the source and made the AspDotNetStorefrontCore.XSLTExtensionBase.LookupProductImage() method just return the string "TEST" expecting any product image to be replaced with "TEST", compiled the solution using Visual Studio's build. It builds successfully and the DLLs in the /web/bin directory now reflect modified dates the same as my build time, but the rendered page does not reflect my changes. I've reset the cache through the ASPDNSF admin page, but still no change.
Any idea what step I'm missing?
So this turned out to be me being an idiot. Many of the methods in XSLTExtensionBase have several overloads, I was just making the change in the wrong overload. Simply compiling the project is all you need to do to deploy, but you have to change the right code for it to render what you want!

PhoneApplicationPage can not resolve StaticResource

At some point Visual Studio (2010) stopped resolving any static Resources (Brushes, Styles, ViewModel locators, etc.) on all PhoneApplicationPage. However any VisualEllement from with a UserControl can see all StaticResources.
Note: All resources worked before and there wasn't any changes to the pages in terms of XAML.
Any ideas why this is happening?
Temp fix: Temporary fix I came up with is to add All resources to PhoneApplicationPage.Resources so before trying to resolve resources at the application level, elements will resolve them from RD of the PhoneApplicationPage.
Edit: All StaticResources are resolved when running in Blend, this issues seems to happen only in VS. Also when creating a new Page in the solution it also can't resolve any StaticResources.
There are a couple of things you could try.
Create a new page. Can you access StaticResources from there? If
you can, you can narrow down the cause to the xaml for the pages
where you are having trouble now. Check the xmlns declarations from
the new [age against the old to verify nothing has been accidentally
removed, for example.
If you have access to Expression Blend, try
opening the solution there and then right-clicking on an element to
apply a resource. I have a feeling that the tool might be able to
fix issues, but never having seen this behaviour before I can't
guarantee it.
Also, are your resources defined in app.xaml, or have you specified another file containing the resources? If the latter, has the plumbing to tie the extra file in gone AWOL somehow?

Resources