ASP.NET MVC 404 Even When Route Exists - asp.net-mvc-3

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.

Related

difference between URL rewriting and URL routing mvc

I am trying to learn MVC and need help in understanding the difference between URL rewriting and URL routing in MVC.
URL Routing is the routing done by MVC which is , When we type something in the url space, then it will first invoke the
RouteConfig.RegisterRoutes(RouteTable.Routes);
which is in Global.asax.cs Application_Start() method if you put a break point in that then you could understand the routing method , then it will call the
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Which is in App_start=>RouteConfig class you can find out the RegisterRoutes method here . Here we can define the structure of our url and here it will decide which controller and action should be invoked.
This process is url Routing
URL rewriting is entire diff concept
Let us assume our controller name is Customer and action name is Login
when we run the application our url will be like the below
http://Localhost:233/Customer/Login
but the client ended http://Localhost:233/Login only, for SEO purposes
In order to achieve this we have to rewrite the url by keeping the same controller and action name
to know more about url rewriting i have explained attribute rewriting in http://grandhah.blogspot.in/2015/07/url-rewriting-in-mvc-4-attribute-routing.html Please visit and let me know you are clarified enough.. good day

Hierarchies in MVC3

Im working my way though an ASP.NET MVC tutorial and couldnt find the answer im looking for.
I understand that each controller class in the 'Controller' root folder is mapped to a Url, so:
****Controller Folder****
|- StoreController.cs
Maps to $url/Store
However, If I wish to creater a 'subfolder'
I.e. a Controller class located for $url/Store/Testing I cant seem to see how I go about it.
I tried deriving a class from StoreController.cs, but that didnt work.
URLs do not necessarily correspond to MVC application internal folder structure. You can use MVC routing tables to conceal the internal structure and redirect specific URLs to any controllers/actions you want. For example, you can create a TestingController.cs class in the Controllers folder and use this route in Global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Store-Testing", // Route name
"Store/Testing/{action}/{id}", // URL with parameters
new { controller = "Testing", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
In this case, a request to http://[domain]/Store/Testing will be handled by TestingController.
That url would with the default route point to an action called Testing, within the Store controller.
You can however create your own custom routes in your global.asax file.

ASP.NET MVC 3 Basic Routing Issue

Works:
http://localhost/ApplicationName/Home/Index
http://localhost/ApplicationName/
http://localhost/ApplicationName/AnotherController/
404 Error:
http://localhost/ApplicationName/Home
I have an Index view for my Home controller and an Index action.
Everything was working fine, then I refactored and changed the application name.
Now only the Home controller won't default to the Index view when the action is left out
Here's my RegisterRoutes in 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 }
);
}
There was an empty folder in the application root named Home. The application was going there first for the view. Deleted it, problem solved.

Troubleshooting "The resource cannot be found." Error

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!

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