简体   繁体   English

ASP.NET MVC中的相对链接和URL重写

[英]Relative links and URL rewrite in ASP.NET MVC

I have an ASP.NET MVC application that's used by different companies. 我有一个由其他公司使用的ASP.NET MVC应用程序。 The URL for the app looks something like this: http://myapp.com/companyA/stuff or http://myapp.com/companyB/stuff . 该应用程序的网址如下所示: http://myapp.com/companyA/stuff : http://myapp.com/companyA/stuffhttp://myapp.com/companyB/stuff I get the company identifier from the URL in Application_BeginRequest and then do a RewritePath to http://myapp.com/stuff . 我从Application_BeginRequest的URL获取公司标识符,然后执行RewritePathhttp://myapp.com/stuff

My question is about relative links that ASP.NET MVC generates. 我的问题是有关ASP.NET MVC生成的相对链接。 It appears that these links are generated to the root of the site, disregarding company identifiers in the URL. 这些链接似乎是生成到站点根目录的,而无视URL中的公司标识符。 For example, once at http://myapp.com/companyA/stuff , @Url.Action("index", "home") creates a link that points to http://myapp.com/ not http://myapp.com/companyA/ 例如,一旦到达http://myapp.com/companyA/stuff @Url.Action("index", "home")就会创建指向http://myapp.com/ not http://myapp.com/companyA/

Is there a way to instruct ASP.NET MVC to use a certain baseURL when generating relative links? 有没有一种方法可以指示ASP.NET MVC在生成相对链接时使用特定的baseURL

Thanks. 谢谢。

The problem is that you are using URL rewriting. 问题是您正在使用URL重写。 URL rewriting is a one-way map - URL to resource. URL重写是一种单向映射-URL到资源。

The modern (and more MVC-centric) way of doing things is to use .NET routing. 现代的(并且更以MVC为中心)的处理方式是使用.NET路由。 Routing is a 2-way map from URL to resource and from resource back to URL. 路由是从URL到资源以及从资源到URL的两向映射。 A resource is identified by a (preferably unique) set of route values. 通过一组(最好是唯一的)路由值来标识资源。

Routing in MVC is very flexible. MVC中的路由非常灵活。 The MapRoute extension method can be used to create quite a range of options. MapRoute扩展方法可用于创建很多选项。 However, you can literally create any URL scheme you want by inheriting either the Route class or RouteBase class. 但是,您可以通过继承Route类或RouteBase类来创建所需的任何URL方案。 You can literally use anything in the request (URL, headers, cookies, session state, etc) to route to a resource (usually a controller action). 您可以从字面上使用请求中的任何内容(URL,标头,cookie,会话状态等)来路由到资源(通常是控制器操作)。 See this answer for an advanced example. 有关高级示例,请参见此答案

But in your case, you can simply set up a route to map to the correct company. 但就您而言,您只需设置一条路线即可映射到正确的公司。

routes.MapRoute(
    name: "Company",
    url: "{company}/{action}",
    defaults: new { controller = "Company", action = "Index" },
    constraints: new { company = @"companyA|companyB" }
);

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

Then you just need a company route (a controller for each company is another option, it depends on how much you want to share). 然后,您只需要一条公司路线(每个公司的控制器是另一种选择,这取决于您要共享多少)。

public class CompanyController : Controller
{
    // CompanyA/
    // CompanyA/Index
    // CompanyB/
    // CompanyB/Index
    public ActionResult Index()
    {
        return View();
    }

    // CompanyA/Stuff
    // CompanyB/Stuff
    public ActionResult Stuff()
    {
        return View();
    }
}

Then in your application, you need to specify the company when building the ActionLinks , which will use the routing framework to build the URL. 然后,在您的应用程序中,在构建ActionLinks时需要指定公司,该公司将使用路由框架来构建URL。

@Url.ActionLink("Home", "Index", "Home", new { company = "companyA" }, null)
@Url.ActionLink("Home", "Index", "Home", new { company = "companyB" }, null)

An alternative option that might be better for this scenario is to use Areas to separate each company. 在这种情况下可能更好的替代方法是使用Areas分开每个公司。 Also see the MSDN tutorial . 另请参阅MSDN教程 Areas each have their own route and they act like different containers within an MVC application. 每个区域都有自己的路线,它们在MVC应用程序中的作用类似于不同的容器。

But either way, you should really get rid of the URL rewriting because that is an antiquated way of working with URLs. 但无论哪种方式,您都应该真正摆脱URL重写,因为这是使用URL的过时方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM