Troubleshooting "The resource cannot be found." Error - asp.net-mvc-3

I have an MVC 3 App with a few Areas built into it, one of the areas is my Admin section of my site. Everything was working just fine. I wanted to try MvcContrib Portable Areas to make my app more modular, so I installed MvcContrib and after some trial and error I got a couple Portable areas up and running.
Then I decided to move my Admin area up into a portable area, so i created the new project and stubbed out my Admin portable area. I had to rename my local Admin Area so that it would not conflict. While moving some code up to the Admin PA I decided that I did not want the headache of moving all the Telerik and other things I had wired up to my Admin area. SO I moved things back down to the main project Area and deleted the Admin PA.
I rewired my Admin Area back in and went over everything involved in setting up an Area. Now for the life of me I cannot get any of my areas in my main project to load. I keep getting the "The resource cannot be found." error message.
I even went as far as removing the reference to MvcContrib and Portable Areas but still no luck. I am at the end of my rope as I do not know how to debug this. I have used a custom route handler as well as Glimpse but neither are very useful when the error is thrown.
Here is the route in my global.asax
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
null,
new string[] { "CoolProject.Web.Controllers" }
);
here is the route in my admin area registration file
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "CoolProject.Web.Areas.Admin.Contollers" }
);
here is my Global.asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "CoolProject.Web.Controllers" }
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
InitializeContainer();
AppStart_Structuremap.Start();
SiteMapManager.SiteMaps.Register<XmlSiteMap>("AdminNavigation", sitemap => sitemap.LoadFrom("/Areas/Admin/AdminNavigation.sitemap"));
}
I have checked my setup against a similar working site and everything is the same with the exception of namespaces and classes.
I am developing on Win 7 IIS7.5
Using Glimpse Routes plugin I can see that the routes exist but the problem is that the route in my Global.axas file is taking over all the requests to the areas.
What do I need to do with my routes to allow for the core app and the areas to get along? The funny thing is I have another production app using areas that works just fine.
Update....
I created a new MVC 3 Project, Added a single area named Admin. Then I edited the AdminAreaRegistration.cs and Global.asax files to include the namespaces in the MapRoute statement, compiled it and it runs perfectly. I can access the area with no problem.
I then compared the Global.asax and AdminAreaRegistration.cs with the files in my broken project and they are Identical. This is not an issue with how I set up my routes, I think there is another problem that I am not seeing.

Are you calling
AreaRegistration.RegisterAllAreas();
on Application_Start in your Global.asax? What server are you using for development Cassini, IISExpress, IIS?
Edit after reviewing more detailed information.
In your Admin Area Registration File if you have this code
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "CoolProject.Web.Areas.Admin.Contollers" }
);
I think there is a typo in "CoolProject.Web.Areas.Admin.Contollers" and it should be "CoolProject.Web.Areas.Admin.Controllers"?

Make sure the namespace is correct in your Controller, as well. If you created the new Area by copying MVC components from another MVC application, for instance (as I did) it's easy to forget to change the namespace!

Related

MVC3 Routing with Areas

I have a MVC3 application with two areas and a root area. The general structure looks like
Root
- Root/Areas/Manager
* Views/Home/Index.cshtml
* ManagerAreaRegistration.cs
- Root/Areas/Participant
* Views/Home/Index.cshtml
* ParticipantAreaRegistraion.cs
- Root
* Views/Home/Index.cshtml
* Views/Account/Register.cshtml
* Global.asax.cs
I am having two problems with routing. The first is that I am unable to navigate to any pages in the Root/Views folders except the one set as default in the Global.asax.cs file. The Global.asax.cs file looks like:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {controller="Home" , action = "Index", id = UrlParameter.Optional },
new[] { "MVCApplication.Controllers" } // for areas
);
...
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
...
And the code in Root/Views/Home/Index.cshtml which is the start page looks like:
#Html.ActionLink("Accounts","Register", new {area="", controller="Accounts"},null)
#Html.ActionLink("Manager", "Index", new { area = "Manager", controller = "Home" })
#Html.ActionLink("Participant", "Index", new { area = "Participant", controller = "Home" })
The two area links work fine as I have added routes into the registration files in each area, but the link to Accounts/Register which is another page in the root gives a 'resources not found error'. However, if I change the Global.asax.cs route to have
new {controller="Accounts" , action = "Register", id = UrlParameter.Optional },
in the default route, then I can start on the Register page.
So my first question is: How do I use routes to be able to access both pages in the Areas and in the Root (ie the Accounts/Register page)?
My second question has to do with the areas themselves. Since they both have a 'Home' controller, I have put the area name in front of one to distinguish it, but I would like to not have to do this. Currently the 'ParticipantAreaRegistration.cs file has the code:
context.MapRoute(
"Participant_default",
"Participant/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "MvcApplication.Areas.Participant.Controllers" } // for areas
);
which gives URL's of "localhost**/Participant/Home"
while the ManagerAreaRegistraion.cs has code
context.MapRoute(
"Manager_default",
"{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "MvcApplication.Areas.Manager.Controllers" } // for areas
);
which gives URL's of "localhost***/Home/"
My second question is : How can I have the URL of "localhost**/Home for both Manager and Participant (or for any number of areas) without having to have the Area name displayed in the URL?
I know this question is similar to others already on file, but I have scoured these to no avail and am currently drowning in inefficiency, so I thought I would try asking with specificity. Thanks.
You can use custom routing.
Something similar to this one:
MVC 2 AreaRegistration Routes Order
Using the solution in the above problem, you can write custom order of routing.
In one of my application, I have areas named Admin,Blog,Members and
Public. I have routed the Public area as the url:
http://localhost:4000/, Admin as: http://localhost:4000/admin, blog
as: http://localhost:4000/blog, etc.. If you want my code, I can give
you.

MVC3 Area +Authorize attribute + Role strange issue

I really don't know what title should I use to describe my problem. To simplify my problem. Here is my test. I create a mvc3 site from scratch. I then add area called "admin". Inside admin, I have a controller named "Search" and has "Authorize" attribute decorated. I then changed my Global.ascx.cs route setting to append my controller namespace. Now I start my test.
Question 1
When I am accessing to http://localhost:xxx/Search page, it redirects me back to /Account/Logon page, it makes me confuse first, why it redirects me to logon page? it shouldn't reach to Admin search controller at all as I understand. If I removed the Authorize attribute, it display the yellow screen said can't find the view as I expected.
Question 2
If I add Authorize attribute with role, e.g. (Roles="Admin"), then I try access to Search page again, no matter login succeed or not, I always get redirect back to logon page. Why it doesn't give me the yellow screen, coz I am trying to request the search controller index view in the main site not the admin area's one. quite confuse.
I am a newbie in MVC development, can someone give me a solution regarding to my problem?
Thanks
Global.ascx.cs
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[]{"TestAreaRouting.Controllers"}
);
}
You could constrain the default controller factory to look only inside the specified namespace for controllers in the RegisterRoutes method of Global.asax by setting the UseNamespaceFallback data token to false:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "TestAreaRouting.Controllers" }
).DataTokens["UseNamespaceFallback"] = false;
}
If you don't do this when you request /search the admin area route doesn't match because the url doesn't start with the Admin prefix.
So it is the default route that matches. The default controller factory starts scanning the assembly for a class called SearchController that derives from Controller and since it finds one it instantiates it and uses it to serve the request. Obviously it doesn't find a corresponding Index view because it looks in ~/Views/Search/Index.cshtml which obviously doesn't exist. The actual view is located in the area.
Now that we have constrained the controllers to their respective locations you could decorate them with the Authorize attribute and it should behave consistently.

ASP.NET MVC 404 Even When Route Exists

I have created a standard ASP.NET MVC 3 Project (Razor) and have not modified Register Routes at all
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
For no reason I can find suddenly none of the urls work, I get a HTTP 404 all the time, both in Cassinin and IIS7 where before it worked... I have tried the RouteDebug tool and it seems to show that the view matches, yet when I turn it off again I once again get 404s
If you not do anything on it, I think just one reason: your IIS has problem.
Sorry I can't suggest solution because this is my first time I see it.

Receive an error when using "Areas" in MVC 3

I want to define two areas in MVC 3 project
"manager and main areas",
manager have some controles like main areas "the controler's Name in both have similar"
but I have implemented different methods in each controler
when I try to run my project, get this error:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /main/home
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
When I implement the project without use "Areas". I never get error, but my project is not clean
I'm assuming in your Global.asax in Application_Start you have:
AreaRegistration.RegisterAllAreas();
as one of the first steps yes?
And in the Area/Main folder you have a MainAreaRegistration.cs which is something like the following:
public class MainAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Main";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Main_default",
"Main/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "MyCompany.Web.Areas.Main.Controllers" }
);
}
}
I've found it necessary to fuly qualify routes with their appropriate namespaces (the namespace the controllers live in) once I have multiple areas to avoid confusion also. Obviously the namespace above is just how I structure mine, though whatever namespace your Main area controllers are in, that's the one to put in the file above.
Hope this helps.
Cheers,
Terry
In Global.asax try to change route to:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "YourNamespace.Controllers" } // ADD THIS
);

Global.asax does not map to Controller Action except for default in ASP.Net MVC2 on IIS5.1

I am a complete newbie,
I have the default to be HomeController->Index
When I hit,
http: / /localhost /SampleApplication
It goes to the index but I have another action "Process" in HomeController.If I hit
http://localhost/SampleApplication/Home/Process returns resource not found.
I am unable to get this in Visual Studio execute/Dev environment or by deploying in IIS.
My Global.asax is,
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
I could not get this going correctly in VS2010 itself.When I execute it launches a development server at port 1048.So I believe its more to do with my understanding or code in global.asax.
You've tagged this with IIS5 yet IIS5 doesn't support your nice MVC URLs out of the box. You will need to use a wildcard extension or you will need to change your urls to contain an extension that you can map to ASP.NET. See
ASP.NET MVC and IIS 5
Deploy ASP.NET MVC on IIS 5.1 (Windows XP)

Resources