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

发布于 2024-09-02 18:42:06 字数 186 浏览 2 评论 0原文

是否可以在 ASP.NET MVC 中根据访问设备/浏览器路由到不同的控制器或操作?

我正在考虑为网站的某些部分设置替代操作和视图,以便从 iPhone 访问网站,以优化其显示和功能。我不想为 iPhone 创建一个完全独立的项目,尽管该网站的大部分内容在任何设备上都很好。

关于如何做到这一点有什么想法吗?

Is it possible, from within ASP.NET MVC, to route to different controllers or actions based on the accessing device/browser?

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. 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.

Any idea on how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷鸟归林 2024-09-09 18:42:06

您可以创建一个路由约束类:

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);
    }
}

然后对其中一个路由强制实施约束,如下所示:

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

然后,您可以创建另一个路由,指向另一个命名空间中具有相同名称的控制器,但具有不同的约束或没有约束。

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.

美男兮 2024-09-09 18:42:06

最好的选择是自定义操作过滤器。

您所要做的就是继承 ActionMethodSelectorAttribute 并重写 IsValidRequest 类。

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

然后在你的控制器中

[IphoneRequest]
public ActionResult Index()

Best bet would be a custom action filter.

All you have to do is inherit from ActionMethodSelectorAttribute, and override the IsValidRequest class.

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文