ASP.NET MVC - 图路线与routes.Add(和404) [英] ASP.NET MVC - MapRoute versus routes.Add (and 404s)

查看:230
本文介绍了ASP.NET MVC - 图路线与routes.Add(和404)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用ASP.NET MVC。

什么是图路线和routes.Add之间的区别?
我只是用图路线?我可以将多个路线图?其中地图以precedence ......那些你叫第一或最后?

我希望能够做类似的东西到计算器确实为用户。
但我想的网址,以适应这种模式:

用户/ {域} / {用户名}被路由到一个UserController的

和所有其他请求做典型的ASP.NET MVC路由。例如:

  routes.MapRoute(
            默认,{控制器} / {行动} / {ID}
            新{控制器=家,行动=索引,ID =}
        );

更新:

当使用网址:<一href=\"http://localhost:3962/User/MYDOMAIN/BTYNDALL\">http://localhost:3962/User/MYDOMAIN/BTYNDALL

我得到的错误: HTTP 404。您正在寻找(或它的一个依赖)的资源可能已被删除,更名或暂时不可用

下面是code我使用的是:

 公共类MvcApplication:System.Web.HttpApplication
{
    公共静态无效的RegisterRoutes(RouteCollection路线)
    {
        routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);        routes.MapRoute(
            用户,
            用户/ {域} / {用户名},
            新{控制器=用户,行动=索引}
        );        routes.MapRoute(
            默认,
            {控制器} / {行动} / {ID}
            新{控制器=家,行动=索引,ID =}
        );    }    保护无效的Application_Start()
    {
        的RegisterRoutes(RouteTable.Routes);
    }
}


解决方案

您的用户控制器应具备

 公共类UserController的:控制器{
    公众的ActionResult指数(字符串域名,用户名字符串){返回查看(); }
}

用户控制器的索引方法的两个变量得到从路由拾起。

I'm just getting started with ASP.NET MVC.

What is the difference between MapRoute and routes.Add ? Should I just be using MapRoute? Can I map multiple routes? Which "maps" take precedence... those you called first or last?

I'd like to be able to do something similiar to the StackOverflow does for users. But I would like the URL to fit this pattern:
"User/{domain}/{username}" to be routed to a UserController

and for all other requests to do the typical ASP.NET MVC routing. ex:

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

UPDATE:
When using the URL: http://localhost:3962/User/MYDOMAIN/BTYNDALL
I get the error: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.

Here is the code I'm using:

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

        routes.MapRoute(
            "User",                                                     
            "User/{domain}/{username}",                           
            new { controller = "User", action = "Index" }      
        );

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

    }

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

解决方案

Your User controller should have

public class UserController : Controller {
    public ActionResult Index(string domain, string username) { return View(); }
}

The two variables on the Index method of the user controller get picked up from the route.

这篇关于ASP.NET MVC - 图路线与routes.Add(和404)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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