简体   繁体   中英

ASP.NET MVC 3 - What is happening with this route?

I'm learning ASP.NET MVC 3 and have a question about custom routes.

If I am in the Global.asax.cs file and create a route like this:

routes.MapRoute(
                "CustomRoute",
                "controller/{name}",
                new { controller = "MyControllerName", action = "Search", id = UrlParameter.Optional }
            );

I'm assuming this means that I would need to explicitly have the word 'controller' in the URL that would then lead to a placeholder called 'name' (which would act as a sort of variable that would take in whatever happened to be at that position in the next part of the URL).

If I had curly brackets around 'controller' - as in, {controller} - that would then turn 'controller' into a place holder (variable?) that would then take in whatever value appears in the URL at that point.

Is this correct? I'm using the term 'placeholder' for lack of a better word - what is the actual name of this?

You're generally correct.

All the words in curly brackets will be treated as variable parts of URL and most of them will be passed to matching controller action as input parameters. controller and action are the special variable names in route - they will be used to determine Controller and Action names respectively.

For example, say you have the route

routes.MapRoute(
                "default",
                "{controller}/{action}/{id}",
                new { controller = "MyControllerName", action = "Search", id = UrlParameter.Optional }
            );

Example matches and corresponding actions:

http://example.com/yourapp/Sample/Test/5 will resolve to controller SampleController ( Controller will be added automatically), action Test . If this action lloks like

public ActionResult Test(int id)
{
...
}

then id parameter will have value 5 inside the method.

http://example.com/yourapp/Sample/Test will be resolved to same action

http://example.com/yourapp/Sample will point to action Search of SampleController .

At last, http://example.com/yourapp/ will point to action Search on MyControllerNameController .

This is an interesting question. I believe that the use of the words "controller", "action" and "id" are part of ASP.NET MVCs "convention over configuration". As you said they are just placeholder variables. You could use other variable names for the parts of a url as long as you were consistent with the naming convention (parameter defaults) when naming controllers throughout your application. For example

routes.MapRoute(
    "Index",
    "{foobar}/{action}/{id}",
    new { foobar= "Home", action = "Index", id= UrlParameter.Optional }
);

Now of course you would have to name your Home controller "HomeFoobar" and all of your other controllers would have to be named "NameFoobar". Your "Foobar" controllers would still have to inherit from Controller.

If you do not use the {curly_braces} around the url parameter names, you will get the following 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: /

-------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM