继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

asp.net mvc中的路径选择

紫衣仙女
关注TA
已关注
手记 231
粉丝 71
获赞 334

MVC的路径选择十分灵活,可以用类似/parm1/parm2/parm3/ 的方式(这个有点象iis的urlrewriter),也可以象传统url那样用/?parm1=a&parm2=b&parm3=c这样访问

关键是Global.asax中Route规则的配置

以下是一个Global.asax的示例:

 1protected void Application_Start(object sender, EventArgs e)
 2        {
 3            // Note: Change Url= to Url="[controller].mvc/[action]/[id]" to enable 
 4            //       automatic support on IIS6
 5
 6            RouteTable.Routes.Add(new Route
 7            {
 8                Url = "[controller]/[action]",
 9                Defaults = new { action = "Index" },
10                RouteHandler = typeof(MvcRouteHandler)
11            });
12
13
14            RouteTable.Routes.Add(new Route
15            {
16                Url = "[controller]/[action]/[id]",
17                Defaults = new { action = "Index", id = (int?)null },
18                RouteHandler = typeof(MvcRouteHandler)
19            });
20
21
22            RouteTable.Routes.Add(new Route
23            {
24                Url = "[controller]/[action]/[id]/[name]",
25                Defaults = new { action = "Index", name = (string)null,id=(int?)null },
26                RouteHandler = typeof(MvcRouteHandler)
27            });
28
29            RouteTable.Routes.Add(new Route
30            {
31                Url = "[controller]/[action]/[id]/[name]/[sex]",
32                Defaults = new { action = "Index", name = (string)null, id = (int?)null,sex=(string)null },
33                RouteHandler = typeof(MvcRouteHandler)
34            });   
35
36
37            RouteTable.Routes.Add(new Route
38            {
39                Url = "Default.aspx",
40                Defaults = new { controller = "Home", action = "Index", id = 2, name = "Jimmy",sex="female" },
41                RouteHandler = typeof(MvcRouteHandler)
42            });
43        }


对应的HomeController文件:

public class HomeController : Controller
    {       
        /// <summary>
        /// Example URL: /Home/Index/?id=2&name=abc&sex=male (对应Url = "[controller]/[action]")                     
        ///              /Home/Index/2/?name=abc&sex=male  (对应Url = "[controller]/[action]/[id]")          
        ///              /Home/Index/2/abc/?sex=male  (对应Url = "[controller]/[action]/[id]/[name]")
        ///              /Home/Index/2/abc/male/ (对应Url = "[controller]/[action]/[id]/[name]/[sex]")
        ///              /Home/Index/2/abc/  (对应Url = "[controller]/[action]/[id]/[name]")
        ///              /Home/Index/2/ (对应Url = "[controller]/[action]/[id]")
        /// </summary>
        /// <param name="id"></param>
        [ControllerAction]
        public void Index(int? id,string name,string sex)
        {
            ViewData["id"] = id;
            ViewData["name"] = name;
            ViewData["sex"] = sex;
            RenderView("Index");
        }      
    }






对应的Index视图:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="MVCDemo.Views.Home.Index" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">

    <h2>Welcome to my ASP.NET MVC Application!</h2>
    
    id=<%=ViewData["id"] %> <br/>
    name=<%=ViewData["name"] as string %><br/>
    sex=<%=ViewData["sex"] as string %>

</asp:Content>

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP