开发者

Overriding the return View when it is a mobile site to use a different view

Would it be possible to override the call to:

return View()

To append the string "mobile_" to the name of the view file, in other words, instead of calling the view "index.aspx" use "mobile_index.aspx".

Somewhere else I guess in a before action fires event I will check if its a mobile application, set a flag. So if the flag is set, it should append "mobile_" to the view name.

And I want this done at the con开发者_如何学编程troller level and not have to do this for each action.


What you want to achieve has been implemented by Stackoverflow.
I was trying to do the same recently and I bumped into this article.
It's really interesting and goes on the same road you want to follow.

At the end I decided to implement for my implementation to go this way.


The linked duplicate question has a number of options for implementing automatic mobile device detection; Scott Hanselman's is MVC3-specific so I'd start there.

However, to implement your exact solution is also possible. Create your own base controller class and override the View() method to do what you want before calling into the base View. Then derive all your site controllers from that.

The source code for MVC3 is published my MS so you should be able to see exactly what the base View() uses to figure out the view name, and "fix" it to be mobile_whatever.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜