在 URL ASP.NET 中传递参数
Passing parameters in URL ASP.NET
我正在创建一个 ASP.NET MVC
应用程序,该应用程序将在 WindowsPhone
、iOS
、Android
应用程序 WebView
控件或 iFramed
在其他网站。我想根据请求我的站点的客户端,以不同方式控制我的 ASP.NET
应用程序的某些行为和样式。
我实现此目的的一种方法是通过 header 等传递此信息,例如 ClientType=WindowsPhone
等,并在我需要的任何地方读取 header 值。但我在想是否有一种干净的方法可以在 URL 中传递此信息。像
https://www.example.com/windowsphone/Manage/Index
https://www.example.com/ios/Manage/Index
并在我的代码中需要的地方从 URL 读取客户端类型。
如果 RouteConfig
可以帮助我,我很困惑。
我可以通过以下方式做到这一点,
routes.MapRoute(
name: "Default",
url: "{clientType}/{controller}/{action}",
defaults: new { controller = "Home", action = "Index" },
constraints: new { clientType = new ClientTypeConstraint() });
我的 ClientTypeConstraint 如下所示
public class ClientTypeConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
// Validate routeValueDictionary["clientType"] here
}
}
我正在创建一个 ASP.NET MVC
应用程序,该应用程序将在 WindowsPhone
、iOS
、Android
应用程序 WebView
控件或 iFramed
在其他网站。我想根据请求我的站点的客户端,以不同方式控制我的 ASP.NET
应用程序的某些行为和样式。
我实现此目的的一种方法是通过 header 等传递此信息,例如 ClientType=WindowsPhone
等,并在我需要的任何地方读取 header 值。但我在想是否有一种干净的方法可以在 URL 中传递此信息。像
https://www.example.com/windowsphone/Manage/Index
https://www.example.com/ios/Manage/Index
并在我的代码中需要的地方从 URL 读取客户端类型。
如果 RouteConfig
可以帮助我,我很困惑。
我可以通过以下方式做到这一点,
routes.MapRoute(
name: "Default",
url: "{clientType}/{controller}/{action}",
defaults: new { controller = "Home", action = "Index" },
constraints: new { clientType = new ClientTypeConstraint() });
我的 ClientTypeConstraint 如下所示
public class ClientTypeConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
// Validate routeValueDictionary["clientType"] here
}
}