ASP.NET网页API 2 - 子域属性路由 [英] ASP.NET Web Api 2 - Subdomain Attribute Routing

查看:133
本文介绍了ASP.NET网页API 2 - 子域属性路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 AttributeRouting 在我的MVC应用程序相当一段时间。但是,有一点它总是缺乏在网页API子域的路由。

(该库等功能,与MVC而不是网页API工作中)

现在我刚才读关于<一个href=\"http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2\"相对=nofollow>在网页API 关于属性路由和它现在包含在网页API开箱即用的新的改进。

不过,我看不提子域路由。难道在网页API支持2?
如果没有,我怎么能得到子路由在我的Web API,以便我可以用打ApiController http://api.mydomain.com/cars/1


解决方案

路由通常用于域/端口后的URL部分

。只要你有你的主机配置,让一个域的Web API处理请求,您应该能够在该域内路由的网址。

如果你想路由是特定领域(如只要求通过一定的途径处理的api.mydomain.com域),您可以使用自定义路由约束。要做到这一点与属性的路由,我认为你需要有:

首先,自定义路由约束类本身。见<一href=\"http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-custom-route-constraint-cs\">http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-custom-route-constraint-cs对于MVC域例子;在Web API接口是稍有不同(<一href=\"http://msdn.microsoft.com/en-us/library/system.web.http.routing.ihttprouteconstraint(v=vs.108).aspx\">http://msdn.microsoft.com/en-us/library/system.web.http.routing.ihttprouteconstraint(v=vs.108).aspx).

二,自定义路线建设者。从HttpRouteBuilder派生并重写BuildHttpRoute方法来添加你的约束。事情是这样的:

 公共类DomainHttpRouteBuilder:HttpRouteBuilder
{
    私人只读字符串_domain;
    公共DomainHttpRouteBuilder(字符串域){_domain =域; }
    公众覆盖IHttpRoute BuildHttpRoute(字符串routeTemplate,IEnumerable的&LT;&列举HTTPMethod GT; httpMethods,串controllerName,串actionName)
    {
        IHttpRoute路线= base.BuildHttpRoute(routeTemplate,httpMethods,controllerName,actionName);
        route.Constraints.Add(域,全新DomainConstraint(_domain));
        返回路线;
    }
}

三,当映射属性的路线,用你的自定义路由生成器(调用了路径生成器过载):

  config.MapHttpAttributeRoutes(新DomainHttpRouteBuilder(api.mydomain.com));

I've been using AttributeRouting for quite some time in my MVC application. However, one thing it always lacked is subdomain routing in Web Api (among other features in that library that work with MVC but not Web Api).

Now I just read about the new improvements in Web Api regarding Attribute Routing and that it's now included with Web Api out of the box.

However, I see no mention of subdomain routing. Is it supported in Web Api 2? If not, how can I get subdomain routing in my Web Api so that I can hit the ApiController using http://api.mydomain.com/cars/1?

解决方案

Routing is normally used for the portion of the URL after the domain/port. As long as you have your host configured to let Web API handle requests for a domain, you should be able to route URLs within that domain.

If you do want routing to be domain-specific (such as only have requests to the api.mydomain.com domain handled by a certain route), you can use a custom route constraint. To do that with attribute routing, I think you'd need to have:

First, The custom route constraint class itself. See http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-custom-route-constraint-cs for an MVC domain example; the Web API interface is slightly different (http://msdn.microsoft.com/en-us/library/system.web.http.routing.ihttprouteconstraint(v=vs.108).aspx).

Second, A custom route builder. Derive from HttpRouteBuilder and override the BuildHttpRoute method to add your constraint. Something like this:

public class DomainHttpRouteBuilder : HttpRouteBuilder
{
    private readonly string _domain;
    public DomainHttpRouteBuilder(string domain) { _domain = domain; }
    public override IHttpRoute BuildHttpRoute(string routeTemplate, IEnumerable<HttpMethod> httpMethods, string controllerName, string actionName)
    {
        IHttpRoute route = base.BuildHttpRoute(routeTemplate, httpMethods, controllerName, actionName);
        route.Constraints.Add("Domain", new DomainConstraint(_domain));
        return route;
    }
}

Third, When mapping attribute routes, use your custom route builder (call the overload that takes a route builder):

config.MapHttpAttributeRoutes(new DomainHttpRouteBuilder("api.mydomain.com"));

这篇关于ASP.NET网页API 2 - 子域属性路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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