tags:

views:

34

answers:

2

This is a beginner level question for asp.net MVC

I have the following code in global.asax.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 = (string)null }  // Parameter defaults
            );



        }

in Homecontroller.cs i have updated the Index method as follows

public ActionResult Index(string id)
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC1!"+ id;

            return View();
        }

My understanding is, if I give the url http://localhost/mvc1/default/1 it should work

instead it is throwing up 404 error

any help what is the reason behind this

+1  A: 

The URL you're requesting is asking for a controller called "mvc1" and an action called "default" which will receive an id of "1". Since you don't have a controller named "mvc1" (I assume?), you're getting the 404 error.

The defaults for controller and action are only used if controller and action aren't provided. Since you provided controller and action, MVC is looking for them specifically.

Patrick Steele
A: 

I'm assuming your application is called "mvc1" and that's the root of your project. If that's the case:

So "default" is the name if your route, not the name of the action. Basically what the routing engine does is look for a controller and action that matches requests coming in. Given the route you have setup, it would break down like this:

http://localhost/MVCApplication1/default/1
                                 (cont)  (action)

If certain parts of the route are omitted, it will attempt to fill in the missing values with the defaults you have specified. As you can see, there is no controller named DefaultController in your project, and thus it uses the default you've specified which is Home. It then tries to find an action method called default and fails again, so it uses the default value in your route, which is Index. Finally, you have 2 segments left in your URL, and no route matches that pattern (2 segments after the action), so it can't find the right place to go.

What you need to do is remove one of your segments, and this should work. Routing can be a little tricky, so I would recommend reading up on it.

Scott Anderson
@Scott thanks for the detailed reply.Yes MVC1 is the application nameI am still not sure about these lines though:Finally, you have 2 segments left in your URL, and no route matches that pattern (2 segments after the action)Bala: default/1 means Controller/action. I donot have any segments after this :(What you need to do is remove one of your segments, and this should work. Routing can be a little tricky, so I would recommend reading up on it.Bala: so what will be the URL i need to enter
balalakshmi
@scottFinally found the answer: I was running all the while on IIS6.0 and did not do the basic tweaking in IISthe below url helped me do thishttp://blog.stevensanderson.com/2008/07/04/options-for-deploying-aspnet-mvc-to-iis-6/thanks for the answer and explanation
balalakshmi