User Agent Switcher for Firefox
If you don’t have access to your computer from mobile device you can use User Agent Switcher add-on for Firefox. This add-on allows you to change user-agent string so your Firefox identifies itself as a browser you say for web servers.User Agent Switcher is set to identify Firefox as iPhone 3.0. You can change settings
and add more user-agent strings if you like. The add-on is free to use.
Creating mobile view
Create new ASP.NET MVC 4 internet application and leave it as it is created by Visual Studio. We will create now one special view for all mobile devices to show how ASP.NET MVC 4 uses display modes.Now create new view and name it as Home.mobile.cshtml. Take the code here using copy and paste to get it to your new mobile view.
Now run your project from Visual Studio and take a look at the page as it is shown in your default web browser. Run Firefox, make it identify itself as iPhone and open same URL in it. You should see the following page.
@{ ViewBag.Title = "Home Page Mobile"; } <h2>@ViewBag.Message</h2> <p> To learn more about ASP.NET MVC visit <a href=http://asp.net/mvc title="ASP.NET MVC Website">http://asp.net/mvc</a>. </p> <ul data-role="listview" data-inset="true"> <li data-role="list-divider">Navigation</li> <li>@Html.ActionLink("About", "About", "Home")</li> <li>@Html.ActionLink("Contact", "Contact", "Home")</li> </ul>
Creating custom display mode rules
When you are targeting wider range of mobile devices or other user agents then you usually face incompatibility problems that different user agents of same type may have. By example, not all mobile web browsers support CSS fully and for older and poorer mobile web browsers you should create special primitive views. Of course, there are also devices like smart phone browsers that are very powerful and you may want to use some UI parts to use some specific features of these browsers.You can define your own display mode rules in Application_Start method of Global.asax. Here is one simple example taken from release notes.
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
DisplayModes.Modes.Insert(0, new DefaultDisplayMode("iPhone")
{
ContextCondition = (context => context.Request.UserAgent.IndexOf
("iPhone", StringComparison.OrdinalIgnoreCase) >= 0)
});
}
@{ ViewBag.Title = "Home Page iPhone";
}
<h2>Welcome to iPhone page</h2>
<p>@ViewBag.Message</p> <ul data-role="listview" data-inset="true">
<li data-role="list-divider">Navigation</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
After running your project and opening it in Firefox you should see something like this.
No comments :
Post a Comment