前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >asp.net mvc中的路径选择

asp.net mvc中的路径选择

作者头像
菩提树下的杨过
发布2018-01-22 17:16:48
1.9K0
发布2018-01-22 17:16:48
举报

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

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

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

代码语言:javascript
复制
protected void Application_Start(object sender, EventArgs e)

        {

            // Note: Change Url= to Url="[controller].mvc/[action]/[id]" to enable 

            //       automatic support on IIS6



            RouteTable.Routes.Add(new Route

            {

                Url = "[controller]/[action]",

                Defaults = new { action = "Index" },

                RouteHandler = typeof(MvcRouteHandler)

            });

            RouteTable.Routes.Add(new Route

            {

                Url = "[controller]/[action]/[id]",

                Defaults = new { action = "Index", id = (int?)null },

                RouteHandler = typeof(MvcRouteHandler)

            });


            RouteTable.Routes.Add(new Route

            {

                Url = "[controller]/[action]/[id]/[name]",

                Defaults = new { action = "Index", name = (string)null,id=(int?)null },

                RouteHandler = typeof(MvcRouteHandler)

            });



            RouteTable.Routes.Add(new Route

            {

                Url = "[controller]/[action]/[id]/[name]/[sex]",

                Defaults = new { action = "Index", name = (string)null, id = (int?)null,sex=(string)null },

                RouteHandler = typeof(MvcRouteHandler)

            });   


            RouteTable.Routes.Add(new Route

            {

                Url = "Default.aspx",

                Defaults = new { controller = "Home", action = "Index", id = 2, name = "Jimmy",sex="female" },

                RouteHandler = typeof(MvcRouteHandler)

            });

        }

对应的HomeController文件:

代码语言:javascript
复制
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视图:

代码语言:javascript
复制
<%@ 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>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2008-04-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档