简体   繁体   English

基于Web浏览器/设备的ASP.NET MVC路由(例如iPhone)

[英]ASP.NET MVC Route based on Web Browser/Device (e.g. iPhone)

Is it possible, from within ASP.NET MVC, to route to different controllers or actions based on the accessing device/browser? 从ASP.NET MVC中,是否可以基于访问设备/浏览器路由到不同的控制器或操作?

I'm thinking of setting up alternative actions and views for some parts of my website in case it is accessed from the iPhone, to optimize display and functionality of it. 我正在考虑为我的网站的某些部分设置替代操作和视图,以防从iPhone访问它,以优化它的显示和功能。 I don't want to create a completely separate project for the iPhone though as the majority of the site is fine on any device. 我不想为iPhone创建一个完全独立的项目,因为大多数网站在任何设备上都可以。

Any idea on how to do this? 有关如何做到这一点的任何想法?

You can create a route constraint class: 您可以创建路径约束类:

public class UserAgentConstraint : IRouteConstraint
{
    private readonly string _requiredUserAgent;

    public UserAgentConstraint(string agentParam)
    {
        _requiredUserAgent = agentParam;
    }
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        return httpContext.Request.UserAgent != null &&
               httpContext.Request.UserAgent.ToLowerInvariant().Contains(_requiredUserAgent);
    }
}

And then enforce the constraint to one of the routes like so: 然后将约束强制执行到其中一条路由,如下所示:

      routes.MapRoute(
           name: "Default",
           url: "{controller}/{action}/{id}",
           defaults: new {id = RouteParameter.Optional},
           constraints: new {customConstraint = new UserAgentConstraint("Chrome")},
           namespaces: new[] {"MyNamespace.MVC"}
           );

You could then create another route pointing to a controller with the same name in another namespace with a different or no constraint. 然后,您可以创建另一个指向具有相同名称的控制器的路径,该控制器具有不同或没有约束的另一个命名空间

Best bet would be a custom action filter. 最好的选择是自定义动作过滤器。

All you have to do is inherit from ActionMethodSelectorAttribute , and override the IsValidRequest class. 您所要做的就是从ActionMethodSelectorAttribute继承,并重写IsValidRequest类。

public class [IphoneRequest] : ActionMethodSelectorAttribute
    {
        public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
        {
             // return true/false if device is iphone

Then in your controller 然后在你的控制器中

[IphoneRequest]
public ActionResult Index()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否将ASP.NET Razor网站(例如MiniBlog)集成为ASP.NET MVC Web应用程序的模块? - Integrate an ASP.NET Razor Web Site (e.g. MiniBlog) as a module of an ASP.NET MVC Web Application? 使用ASP.NET MVC2创建非ASPX视图(例如WML) - Create non aspx views (e.g. WML) with ASP.NET MVC2 ASP.NET MVC C#-使用数组值遍历模型字段,例如model。[Array Value] - ASP.NET MVC C# - Using array values to loop through model fields e.g. model.[Array Value] 从 ASP.NET CORE Web Api 获取 React 中的特定响应标头(例如,Content-Disposition) - Get a specific response header (e.g., Content-Disposition) in React from an ASP.NET CORE Web Api 如何在asp.net中获取国家/地区代码(例如+61)? - How to get country code (e.g. +61) in asp.net? 从跨域 http.get 请求的 ASP.NET Web API 2 响应中获取 Angular 中的特定响应标头(例如,Content-Disposition) - Get a specific response header (e.g., Content-Disposition) in Angular from an ASP.NET Web API 2 response for a cross-origin http.get request 在网络浏览器之外创建cookie(例如,使用VBScript) - Creating a cookie outside of a web-browser (E.g., With VBScript) iPhone iPad未在ASP.Net MVC应用中检测到浏览器类型 - iPhone iPad Not detecting the browser type in ASP.Net MVC app ASP.NET MVC4 Web ZDB974238714CA8DE634A7CE1D08 中的 URL 路由 - URL route in ASP.NET MVC4 Web API 使用Web API和ASP.NET MVC的综合路线? - A catch-all route with Web API and ASP.NET MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM