asp.net-mvc – 通过子域名到mvc或api的ASP.NET路由
发布时间:2020-05-23 22:23:31 所属栏目: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-create-custom-r
|
我们的应用程序有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 MVC应用程序处理程序映射和模块的性能和安全性
- asp.net-core – .Net CORE Dapper连接字符串?
- asp.net-mvc – 自定义模型绑定,模型状态和数据注释
- 为什么ASP.NET抛出这么多异常?
- ASP.net RequiredFieldValidator不阻止回发
- asp.net-mvc – ASP.NET MVC中的动态网站地图
- 在IIS Express中无法启动asp.net核心Web应用程序
- asp.net-mvc – MVC 4 Ajax.beginform提交 – 导致完全回发
- asp.net-mvc-3 – 如何在Razor视图引擎中注册程序集
- asp.net – ASP:NET确认密码字段
推荐文章
站长推荐
- asp.net-mvc-4 – WepApi控制器是否应该返回view
- 没有密码的Asp.net Memebership授权
- 本地化 – 了解MVC6 RC1中的资源文件
- asp.net-mvc如何更改宽度Html.TextBox
- Asp.net MVC5与Bootstrap EditorFor尺寸
- asp.net – 生成第二个标题标签的母版页
- asp.net-web-api – ASP.NET Web API中的回调方法
- ASP.NET MVC Controller FileContent ActionResu
- asp.net-mvc – 在.NET 3.5 ASP.NET MVC应用程序
- asp.net – 如何在SQL4的log4net配置中使用存储过
热点阅读
