asp.net-mvc – 通过子域名到mvc或api的ASP.NET路由
发布时间:2020-12-16 00:03:04 所属栏目:asp.Net 来源:网络整理
导读:我们的应用程序有2个域名(www | api).mydomain.com 如何将请求路由到api.mydomain.com到api控制器和www到mvc控制器? 谢谢 解决方法 我使用约束解决了我的问题. 这个网站给了我线索:http://stephenwalther.com/archive/2008/08/07/asp-net-mvc-tip-30-creat
我们的应用程序有2个域名(www | api).mydomain.com
如何将请求路由到api.mydomain.com到api控制器和www到mvc控制器? 谢谢 解决方法
我使用约束解决了我的问题.
这个网站给了我线索:http://stephenwalther.com/archive/2008/08/07/asp-net-mvc-tip-30-create-custom-route-constraints.aspx 这是我的实施: public class SubdomainRouteConstraint : IRouteConstraint { private readonly string _subdomain; public SubdomainRouteConstraint(string subdomain) { _subdomain = subdomain; } public bool Match(HttpContextBase httpContext,Route route,string parameterName,RouteValueDictionary values,RouteDirection routeDirection) { return httpContext.Request.Url != null && httpContext.Request.Url.Host.StartsWith(_subdomain); } } 我的路线: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default",url: "{controller}/{action}/{id}",defaults: new { controller = "Home",action = "Index",id = UrlParameter.Optional } #if !DEBUG,constraints: new { subdomain = new SubdomainRouteConstraint("www") } #endif ); } public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi",#if DEBUG routeTemplate: "api/{controller}/{id}",#else routeTemplate: "{controller}/{id}",#endif defaults: new {id = RouteParameter.Optional} #if !DEBUG,constraints: new {subdomain = new SubdomainRouteConstraint("api")} #endif ); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何为ASP.NET Core Angular应用程序设置默认端口
- asp.net 结合mysql存储过程进行分页代码
- ASP.NET Core jquery Autocomplete返回列表中的空白行
- asp.net – 分析器错误消息:无法生成代码.抛出了类型’Sys
- 用枚举填充ASP.NET MVC中的SelectList
- asp.net-mvc – ASP.NET MVC自动解码来自AJAX的JSON编码参数
- asp.net-mvc-4 – MVC4不要在重定向上使用主布局
- 在ASP.Net中获取会话ID
- 自签名ASP.NET Web API 2 REST服务的令牌身份验证和授权
- .net – 在IIS中使用IP地址绑定子应用程序
推荐文章
站长推荐
- asp.net-mvc – Razor中的Direct Model.Property
- asp.net-mvc – 没有其他用户的DB上的DbUpdateCo
- Asp.net webForm设置允许表单提交Html的方法
- 如何获得ASP .NET中客户端的临时端口号?
- asp.net-mvc – NSubstitute:在MVC / Web Api控
- asp.net – 我们可以在单个元素上有多个itemprop
- asp.net(c#)动态修改webservice的地址和端口(动
- asp.net-mvc – 将MVC内容文件夹放入Area
- asp.net-mvc-3 – 实体框架,MVC 3,LINQ To Entit
- asp.net-mvc – 当ModelState为InValid时调用的A
热点阅读