zoukankan      html  css  js  c++  java
  • MVC 3 RoutingSystem Kevin

    The routing system has two functions:

    1. Examine an Incoming URL and figure out for which controller and action the request is intended.

    2.Generate outgoing URLs.These are URLs that appear in the HTML rendered from our views so that a specific action will be invoked when the user clicks the link.

    The Routing System Assembly:

    Althoug the routing system is needed by the ASP.NET MVC Framework,it is intended to be used with other APS.NET technologies as well,including WEB FORMs.Because of this, the routing system classes are in the System.Web assembly and not in Syetem.Web.Mvc;

    Creating the Routing Project

    Routes are defined in Global.asax.The routing system doesn't have any special knowledge of controller and actions.It just extracts values for the segment variables and passes them along the request pipeline.It is later in the pipeline,when the request reaches the MVC Framewrok proper,that meaning is assigned to the controller and action variables.This is why routing system can be used with WebForms and how we are able to create our own variables.

    Creating and Registering a Simple Route

    There are two ways to define a Router:

    public static void RegisterRoutes(RouteCollection routes) {
    Route myRoute = new Route("{controller}/{action}", new MvcRouteHandler());
    routes.Add("MyRoute", myRoute);
    }

    This is the first way ,Here is other way:

    public static void RegisterRoutes(RouteCollection routes) {
    routes.MapRoute("MyRoute", "{controller}/{action}");
    }

    Tip:Naming your routes is optional,and there is an argument that doing so sacrifices some of the clean separation of concerns that otherwise comes from routing.We are pretty relaxed about naming,but we explain why this can be a problem in the "Generating a URL from a Specific Route" section later in this chapter.

    Definging Default Values

    public static void RegisterRoutes(RouteCollection routes) {
    routes.MapRoute("MyRoute", "{controller}/{action}", new { action = "Index" });
    }

    Using static URL Segments

    Not all of the segments in a URL pattern need to be variables.You can also create patterns that have static segtments. Suppose we want to match a URL like this to support URLs that are prefixed with PUBLIC;

    Http://mydomain.com/Public/Home/Index

    We can do so by using a pattern like the one shown in Listing blow.

    public static void RegisterRoutes(RouteCollection routes) {
    routes.MapRoute("MyRoute", "{controller}/{action}",
    new { controller = "Home", action = "Index" });
    routes.MapRoute("", "Public/{controller}/{action}",
    new { controller = "Home", action = "Index" });
    }

    Defining Custom Segment Variables

    Caution:Some names are reserved and not avaliable for custom segment variable naems.These are controller,action,and area.The meaning of the first two are obvious,and we will explain the role of areas in the "Working with Areas" section later in this chapter.

    Using Custom Variables as Action Method Parameters

    Using the RouteData.Values property is only one way to access custom route variables.The other way is much more elegant.

  • 相关阅读:
    核主成分分析方法(KPCA)怎么理解?
    通过主成分分析方法进行降维
    线性回归分析中的假设检验
    机器学习中的逻辑回归方法
    关联分析中寻找频繁项集的FP-growth方法
    机器学习中的模型选择和特征选择的基本方法
    计算学习理论中泛化误差的研究
    《幸福的陷阱》读书笔记
    人生规划的逆向思维
    为什么相敬如宾是对的?
  • 原文地址:https://www.cnblogs.com/kfx2007/p/2586809.html
Copyright © 2011-2022 走看看