ASP. NET MVC Learning one route

Source: Internet
Author: User

All development environments for this study are VS2012+MVC4 one: what is MVC:

Let's say you're learning MVC for the first time, okay? We don't understand how it's going to function inside, we just start with the letter meaning of its surface.

M:models (model) V:view (view) c:controllers (Controller)

Don't dwell on what these three letters are, and what is the use of these three things! All we have to do is write down the three letters first! Oh, is not a bit of rote bright, I feel so, a little want to throw bricks feel! (Paralysis, Bo Master want to talk about!!!) -__-!)

Two: The route of MVC learning

Create a new test project home and add a controllers named Homecontrollers, and add an index view for it;

Project file:

Double-click Open index.cshtml View to change its code:

@{    "Index";} 

Run the project we've found that the program works correctly

Perhaps you might be amazed at how MVC works and finds its working files.

Oh, smart you may have guessed that the next step is the advent of the route .

Double-click the Routeconfig file

 Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional}); }

This is the routing mechanism defined internally by MVC.

Routes. Ignoreroute ("{resource}.axd/{*pathinfo}");

The meaning of the sentence code is first understood as
Ignore all axd resources and directly URL access
routes. MapRoute (                "Default",                "{Controller}/{action}/{id}  ",                new"Home""Index  ", id = urlparameter.optional}            );

This is a simple routing rule that defines the name of the route, the URL defines the mapping mechanism for the route, and defaults defines the default parameters for the route

The actual {controller} is the name of the controller that corresponds to our front home,action corresponds to the index we defined earlier, so we enter in the browser

Www.Test.com/Home/Index can successfully see the effect!

All right, here we go, define a route that belongs to us.

routes. MapRoute (                "myroute",                "{controller}-{action} "                 );
Here I don't define the default properties for Myroute's route, of course, we run the project, the project is not found after initialization, manually enter in the browser

Www.Test.com/Home-Index will be able to display the view correctly, to this you can not appreciate an advantage of MVC, of course, you can define more personalized URL path convenient SEO

routes. MapRoute (Name:"Myroute", URL:"{controller}-{action}"                ); Routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional});

Here you need to add that if you define two routing rules in this project, MVC will automatically match the routing rules from top to bottom, and if the Myroute routing rules do not match the default routing rules, if none of the two matches, then an error will be made!

Simple constraints on route values

Just now on the route named "Default" on id = urlparameter.optional, which means ID is optional input, when we remove id = Urlparameter.optional, the operation of the project found an error, the URL to the address plus WWW.TEST.COM/HOME/INDEX/1 route correct.

routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional},//Plus conditional constraints on route valuesConstraintsNew{ID=@"\d+"                }            );

At this point we add a constraint to the id attribute of the router that specifies that the ID can only be numeric, if at this time in the browser input

Www.Test.com/Home/Index/1test URL will be an error

Attach a few routing rules that you've summed up.

Routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional}, Constraints:New{controller =@"\d{4}" }               //constraints:new {Controller [email protected] "\d{4}"}                ); //hotels/All matchesroutes. MapRoute (Name:"Hotel Home", URL:"Home/{*values}", defaults:New{controller ="Home", action ="Index", Hotelid ="" }            ); Routes. MapRoute (Name:"website Home", URL:"{*values}", defaults:New{controller ="Home", action ="Index" }                );

ASP. NET MVC Learning one route

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.