基于Web浏览器/设备(例如iPhone)的ASP.NET MVC路由
是否可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
混合:使用 ASP.NET MVC 的移动网站和移动浏览器定义文件
不知道上面的内容是否有帮助,因为我还没有看过。
还有这个;
我将如何更改 ASP.NET MVC基于设备类型的视图?
Mix: Mobile Web Sites with ASP.NET MVC and the Mobile Browser Definition File
Don't know if the above helps as I havn't watched it yet.
And this one;
How Would I Change ASP.NET MVC Views Based on Device Type?
您可以创建一个路由约束类:
然后对其中一个路由强制实施约束,如下所示:
然后,您可以创建另一个路由,指向另一个命名空间中具有相同名称的控制器,但具有不同的约束或没有约束。
You can create a route constraint class:
And then enforce the constraint to one of the routes like so:
You could then create another route pointing to a controller with the same name in another namespace with a different or no constraint.
最好的选择是自定义操作过滤器。
您所要做的就是继承
ActionMethodSelectorAttribute
并重写IsValidRequest
类。然后在你的控制器中
Best bet would be a custom action filter.
All you have to do is inherit from
ActionMethodSelectorAttribute
, and override theIsValidRequest
class.Then in your controller