另一个MVC路由问题 [英] Another MVC routing problem

查看:127
本文介绍了另一个MVC路由问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一天,我会理解的路由,但是这是我有:

 公共静态无效的RegisterRoutes(RouteCollection路线){
            routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);
            routes.IgnoreRoute(的favicon.ico);            routes.MapRoute(
                默认,//路线名称
                {控制器} / {行动} / {牌照code},// URL带参数
                新{控制器=家,行动=索引,牌照code = UrlParameter.Optional} //参数默认
            );        }

如果我去的http://本地主机一切正常。

如果我去的http://本地主机/首页/索引/ 1234 的一切正常

如果我去的http://本地主机/ 1234 它404的

我试过菲尔哈克的路线调试器,而是因为它抛出一个404路线调试器不工作。

什么我必须做在的RegisterRoutes为的http://本地主机/ 1234 工作


解决方案

  routes.MapRoute(
    许可证code,
    {牌照code}
    新{控制器=家,行动=索引,牌照code = UrlParameter.Optional} //参数默认
);

然后 / 1234 将路由到首页的首页操作控制器:

 公众的ActionResult指数(字符串许可证code)
{
    ...
}

One day I'll understand routing but this is what I have:

public static void RegisterRoutes(RouteCollection routes) {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("favicon.ico");



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

        }

If I go to http://localhost all is ok

If I go to http://localhost/Home/Index/1234 all is ok

if I go to http://localhost/1234 it 404's

I tried Phil Haack's route debugger but because it throws a 404 the route debugger doesnt work.

What do I have to do In RegisterRoutes for http://localhost/1234 to work

解决方案

routes.MapRoute(
    "LicenceCode",
    "{LicenceCode}"
    new { controller = "Home", action = "Index", LicenceCode = UrlParameter.Optional } // Parameter defaults
);

Then /1234 will route to the Index action of Home controller:

public ActionResult Index(string licenceCode)
{
    ...
}

这篇关于另一个MVC路由问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆