在 ASP .NET MVC 3 中使用子域路由生成 URL

发布于 2024-12-14 08:44:50 字数 959 浏览 2 评论 0原文

有很多关于 ASP.NET MVC 中子域路由的材料。其中一些使用区域作为子域的目标,其他则使用其他控制器。

其中有一些:

使用 ASP.NET MVC 的单个应用程序的子域

Asp.Net MVC 2 将子域路由到区域

MVC 3 子域路由

Github 上的 MVC-Subdomain-Routing

它们都解释了如何接受和路由带有子域的请求。

但是:

  1. 它们都没有解释如何生成带有子域的 URL。即我尝试了 @Html.RouteLink("link to SubDomain", "SubdomainRouteName") 但它忽略了子域并生成没有它的 url

  2. 如何处理来自不同区域的相同名称的控制器。所有这些解决方案(它们为此目的使用命名空间)都会抛出存在多个控制器的异常,并建议使用命名空间:)

目的: 使用子域创建网站的移动版本

There are a lot of material written about Subdomain routing in ASP.NET MVC. Some of them use Areas as target for subdomains other use another Controllers.

There are some of them:

Subdomains for a single application with ASP.NET MVC

Asp.Net MVC 2 Routing SubDomains to Areas

MVC 3 Subdomain Routing

MVC-Subdomain-Routing on Github

They do all explain how to accept and route requests with subdomain.

But:

  1. None of them explains how to generate URLs with subdomain. I.e. I tried @Html.RouteLink("link to SubDomain", "SubdomainRouteName") but what it ignores subdomain and generates url without it

  2. How to deal with the same names of controllers from different areas. All those solutions (they use namespaces for these purpose) throw exception that exist several controllers and suggest using namespaces :)

Purpose:
create mobile version of site using subdomain

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小糖芽 2024-12-21 08:44:51

我写了一篇文章介绍如何我在我的应用程序中使用子域路由。源代码可在帖子中找到,但我将尝试解释我是如何执行自定义 RouteLink 方法的。

该辅助方法使用 RouteTable 类根据当前 Url 获取 Route 对象,并将其转换为 SubdomainRoute 对象。

就我而言,所有路由都是使用 SubdomainRoute 定义的,每次我需要添加指向其他页面的链接时,我都会使用自定义 RouteLink 帮助器,这就是为什么我认为此转换是安全的。有了可用的 SubdomainRoute 变量,我就可以获取子域名,然后使用 UriBuilder 类构建 Url。

这是我当前正在使用的代码。

public static IHtmlString AdvRouteLink(this HtmlHelper htmlHelper, string linkText, string routeName, object routeValues, object htmlAttributes)
{
    RouteValueDictionary routeValueDict = new RouteValueDictionary(routeValues);
    var request = htmlHelper.ViewContext.RequestContext.HttpContext.Request;
    string host = request.IsLocal ? request.Headers["Host"] : request.Url.Host;
    if (host.IndexOf(":") >= 0)
        host = host.Substring(0, host.IndexOf(":"));

    string url = UrlHelper.GenerateUrl(routeName, null, null, routeValueDict, RouteTable.Routes, htmlHelper.ViewContext.RequestContext, false);
    var virtualPathData = RouteTable.Routes.GetVirtualPathForArea(htmlHelper.ViewContext.RequestContext, routeName, routeValueDict);

    var route = virtualPathData.Route as SubdomainRoute;

    string actualSubdomain = SubdomainRoute.GetSubdomain(host);
    if (!string.IsNullOrEmpty(actualSubdomain))
        host = host.Substring(host.IndexOf(".") + 1);

    if (!string.IsNullOrEmpty(route.Subdomain))
        host = string.Concat(route.Subdomain, ".", host);
    else
        host = host.Substring(host.IndexOf(".") + 1);

    UriBuilder builder = new UriBuilder(request.Url.Scheme, host, 80, url);

    if (request.IsLocal)
        builder.Port = request.Url.Port;

    url = builder.Uri.ToString();

    return htmlHelper.Link(linkText, url, htmlAttributes);
}

private static IHtmlString Link(this HtmlHelper htmlHelper, string text, string url, object htmlAttributes)
{
    TagBuilder tag = new TagBuilder("a");
    tag.Attributes.Add("href", url);
    tag.InnerHtml = text;
    tag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
    return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}

I've wrote a post on how I use subdomain routing in my application. The source code is available on the post, but I'll try to explain how I did my custom RouteLink method.

The helper method uses the RouteTable class to get the Route object based on the current Url and cast it to a SubdomainRoute object.

In my case all routes are defined using the SubdomainRoute and everytime I need to add a link to some other page I use my custom RouteLink helper, this is why I consider this cast safe. With the SubdomainRoute variable available I'm able to get the subdomain name and then build the Url using the UriBuilder class.

This is the code I'm currently using.

public static IHtmlString AdvRouteLink(this HtmlHelper htmlHelper, string linkText, string routeName, object routeValues, object htmlAttributes)
{
    RouteValueDictionary routeValueDict = new RouteValueDictionary(routeValues);
    var request = htmlHelper.ViewContext.RequestContext.HttpContext.Request;
    string host = request.IsLocal ? request.Headers["Host"] : request.Url.Host;
    if (host.IndexOf(":") >= 0)
        host = host.Substring(0, host.IndexOf(":"));

    string url = UrlHelper.GenerateUrl(routeName, null, null, routeValueDict, RouteTable.Routes, htmlHelper.ViewContext.RequestContext, false);
    var virtualPathData = RouteTable.Routes.GetVirtualPathForArea(htmlHelper.ViewContext.RequestContext, routeName, routeValueDict);

    var route = virtualPathData.Route as SubdomainRoute;

    string actualSubdomain = SubdomainRoute.GetSubdomain(host);
    if (!string.IsNullOrEmpty(actualSubdomain))
        host = host.Substring(host.IndexOf(".") + 1);

    if (!string.IsNullOrEmpty(route.Subdomain))
        host = string.Concat(route.Subdomain, ".", host);
    else
        host = host.Substring(host.IndexOf(".") + 1);

    UriBuilder builder = new UriBuilder(request.Url.Scheme, host, 80, url);

    if (request.IsLocal)
        builder.Port = request.Url.Port;

    url = builder.Uri.ToString();

    return htmlHelper.Link(linkText, url, htmlAttributes);
}

private static IHtmlString Link(this HtmlHelper htmlHelper, string text, string url, object htmlAttributes)
{
    TagBuilder tag = new TagBuilder("a");
    tag.Attributes.Add("href", url);
    tag.InnerHtml = text;
    tag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
    return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文