添加一个简单的路线

时间:2009-11-13 08:43:28

标签: asp.net-mvc

我正在尝试添加一个显示基于字符串参数的数据的路由,如下所示:

http://whatever.com/View/078x756 

如何创建这条简单的路线以及放置它的位置?

3 个答案:

答案 0 :(得分:2)

global.asax.cs文件中,添加以下行:

routes.mapRoute(
    // The name of the new route
    "NewRoute",

    // The url pattern
    "View/{id}",

    // Defaulte route data                                     
    new { controller = "Home", action = "Index", id = "078x756" });

确保在注册默认路由之前添加 - ASP.NET MVC框架将按顺序查看路由并获取与您的URL匹配的第一个路径。 Phil Haack的Routing Debugger是解决此问题的宝贵工具。

答案 1 :(得分:1)

Global.asax中的Application_Start方法中的路由通常为configured。对于您的特定情况,您可以在默认值之前添加路线:

routes.MapRoute(
    "Views",
    "View/{id}",
    new
    {
        controller = "somecontroller",
        action = "someaction",
        id = ""
    }
);

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new
    {
        controller = "home",
        action = "index",
        id = ""
    }
);

答案 2 :(得分:1)

路线已添加到global.asax.cs

添加路线的示例:

namespace MvcApplication1
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );


            routes.MapRoute(
               "WhatEver"
               "{View}/{id}",
               new {controller = "Home","action = "Index", id="abcdef"}
            );

        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }
    }
}
}