在路线MVC 4添加公司名称 [英] Adding company name in Routes mvc 4

查看:83
本文介绍了在路线MVC 4添加公司名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图给选项,用户如Facebook在URL中添加他们的公司名称:

I have been trying to give options to users like Facebook to add their company name in the URL:

http://localhost:50753/MyCompany/Login

我尝试了多种不同的网址,但没有奏效。

I have tried different URLs, but it didn't work.

routes.MapRoute(
                name: "Default",
                url: "{companyName}/{controller}/{action}",
                defaults: new { controller = "Login", action = "Index"}
            );

routes.MapRoute(
               name: "Login",
               url: "{controller}/{action}/{id}",
               defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
           );

现在,当我加入这条路线得到它的工作,我所有的AJAX请求启动失败,那些重新present HTML而不是JSON成功。我已经注意到的是,由于这条路线,我的网页得到重新加载。

Now when I add this route to get it work, all of my AJAX requests start fails and those that succeed represent HTML rather than JSON. What I have noticed is that because of this route, my page gets reload again.

有人可以帮助我弄清楚怎么能使用MVC路由来完成(如果可能的话,或者如果我想在错误的方式)?

Can someone help me figure out how can it be done using MVC routing (if it's possible, or if I'm thinking in the wrong way)?

推荐答案

您所遇到的问题是由于这样的事实,这两种路线将匹配有1,2或3定义的段(所有URL因为控制器和行动有默认值)。由于路由,以便从顶部路线执行到底的路线,你的顶级路线将的总是的匹配和利润路线的从不的将匹配(除了主页)。

The problem you are having is due to the fact that both of these routes will match all URLs that have 1, 2, or 3 segments defined (because the controller and action have default values). Since routes are executed in order from the top route to the bottom route, your top route will always match and your bottom route never will match (except for the home page).

由于上面的路线总是匹配,即假设第一部分是控制器和第二部分是因为你把这些值插入的companyName操作将失败网址控制器路线按键,分别为。

Since the top route always matches, URLs that assume that the first segment is the controller and the second segment is the action will fail because you are putting these values into the companyName and controller route keys, respectively.

有关此为您所期望的工作,你需要做一个路由约束知道所有的公司名称的。

For this to work as you expect, you need to make a route constraint that is aware of all of the company names.

routes.MapRoute(
            name: "Default",
            url: "{companyName}/{controller}/{action}",
            defaults: new { controller = "Login", action = "Index"},
            constraints: new { companyName = "Company1|Comany2|Company3" }
        );

请注意,您可以实现<一个href=\"https://msdn.microsoft.com/en-us/library/system.web.routing.irouteconstraint%28v=vs.110%29.aspx\"><$c$c>IRouteConstraint所以你可以拉动值从缓存数据库模型,而不是硬编码他们进入配置相匹配。见<一href=\"http://www.prideparrot.com/blog/archive/2012/3/creating_custom_route_constraints_in_asp_net_mvc\">this帖子学习如何创建一个自定义路由约束。

Note that you could implement IRouteConstraint so you could pull the values to match from a cached database model instead of hard-coding them into the configuration. See this post to learn how to create a custom route constraint.

或者,正如安迪提到的,你可以通过明确指定URL的1个或多个片段进行匹配独一无二的。

Or, as Andy mentioned, you can make the match unique by specifying 1 or more segments of the URL explicitly.

url: "{companyName}/Login"

这个想法是有一定的部分的办法,使在某些情况下,你所定义的第一条路线的的匹配。

The idea is there must be some way to make the first route you defined not match in certain cases.

另外,你可以实现 RouteBase ,但你只需要如果您需要在比这个简单的场景匹配过程更加控制。

Alternatively, you could implement RouteBase, but you would only need to if you require much more control over the matching process than this simple scenario.

这篇关于在路线MVC 4添加公司名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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