What are some best practices for detecting mobile devices in mvc3 - asp.net-mvc-3

I am enabling one of my sites to redirect certain pages to mobile versions. Ive read blog posts by Steve Sanderson and Shiju Varghese regarding Attribute usage and 51degrees.mobi.
What I would like to have happen is a given page, dealer-locator, redirect to /mobile/dealer-locator if the device is a mobile device. To accomplish this, I have set up an Area with the dealerlocator controller and the necessary views. Ive tested the code directly and it seems to work properly.
When I visit the site from an actual device or FF with the user agent changed to iPhone 3.0, I get redirected to /mobile. Is there a simple way to map page routes to mobile routes?
I should mention that the site uses isapi_rewrite and the page in question has rules that map /dealer-locator to /dealerlocator and dealerlocator is a controller.
Ive also noticed that form posts don't appear to work properly so it does a get for now.

From Shiju Varghese's blog post (which your question mentions), the method of the RedirectMobileDevicesToMobileAreaAttribute class:
// Override this method if you want to customize the controller/action/parameters to which
// mobile users would be redirected. This lets you redirect users to the mobile equivalent
// of whatever resource they originally requested.
protected virtual RouteValueDictionary GetRedirectionRouteValues(RequestContext requestContext)
{
return new RouteValueDictionary(new { area = "Mobile", controller = "Home", action = "Index" });
}
can be overridden to take care of this.
In his example, everything appears to be sent to ~/Mobile/Home/Index, but you can send the request anywhere. He's creating a new route table, equivelant to the one you'd define in Global.asax.cs for your non-mobile routes. You could either replace your entire route table, or loop through your existing routes and return a dictionary that only contains the mobile equivelants.

Related

In ASP .NET Core Razor Pages, how do I properly present server responses to the user?

Recently I have started to deal with dynamic content in my RazorPages web application. I found out about AJAX and have started to incorporate it into my application since it helps me display information to the user. For example, when a user presses a button, some information is taken from an input field and sent to the server, as usual. I use the server response (OnGet or OnPost return value) to display information to the user without them reloading the page.
However, I cannot figure out how to incorporate model binding into this. When I use AJAX I can build the message myself but the model binder of RazorPages was made to this automatically - which is great and I use it a lot, but how can I access the return value of my OnGet or OnPost when I created a form and used tag helpers to allow model binding? I know that I can return Content(string) in these methods but that will show an empty page with the string I return, of course.
My question is: How can I access such a string to display it to the user? Am I even on the right track here or is there maybe a better way to solve this kind of issue?
I would recommend checking out some of the Razor Pages tutorials as they cover the basics of posting form data using Razor pages:
https://www.learnrazorpages.com/razor-pages/forms
Best of luck on your programming journey.

How to cache normal and mobile view for same URL in Codeigniter?

I'm running a check if the user is browsing on a mobile device or desktop, and based on that I call the normal_view for the given URL, or mobile_view for the same URL. So the URL and the controller function are the same, just the view is different (mobile or desktop).
I use Codeigniter built-in Output cache, but if the first user that will open the page, loads the normal_view, it puts it into output cache, and till the cache expire, only that view is loaded no matter if the user is browsing from Mobile or PC.
How can I solve this without making different URLs for mobile and desktop version? Is there any way that I can cache the views separately?
By using User Agent Class Library in Codeigniter we can implement like that
Ex:
$this->load->library('user_agent');
if ($this->agent->is_mobile('iphone'))
{
$this->load->view('iphone/home'); //iphone view page
}
else if ($this->agent->is_mobile())
{
$this->load->view('mobile/home'); //mobile view page
}
else
{
$this->load->view('web/home'); // web page view
}
Note: My Suggestion is can you please use any CSS Frame work like Bootstrap
no need to develop separate for mobile views.It can take care based on device sizes
http://getbootstrap.com
Bootstrap easily and efficiently scales your websites and applications with a single code base, from phones to tablets to desktops with CSS media queries.

Redirecting issues with spring and springmobile

I'm building a website using Spring and the SpringMobile extension.
I've configured the LiteDeviceDelegatingViewResolver with mobilePrefix = m/, tabletPrefix = t/ and enableFallback = true, so that, whenever a dedicated mobile (or tablet) view isn't available, the "normal" (desktop site) view is served.
The site has several sections and subsections. For instance, there's a Products section and several Categories within it.
Currently, there isn't a landing page for Products, so I redirect the user to the first Category. In other words, when the link to /products is clicked, the corresponding #Controller method redirects to /products/category1 (by returning "redirect:/products/category1"); then, the method mapped to /products/category1, serves the view (by returning "viewname").
This works as expected when using the desktop site. However, in the mobile site, whenever I try to browse to /products, I get automatically redirected to /m/products by SpringMobile (which is fine), but then my #Controller gets into action, and, instead of being eventually redirected to /m/products/category1, strangely I get redirected to /m/products/m//products/category1.
Any ideas on why is this happening, and on how to achieve the expected behavior?
P.S.: If I navigate directly to /m/products/category1, the desktop view (the fallback) is properly shown.
Well, it turns out this was a bug in SpringMobile(MOBILE-70, MOBILE-78) which was fixed in version 1.1.0.RC1. It also affected forward: redirection.
I've updated my project dependencies, and the issues appear to be gone for good.

MVC3 Reverse Routing

I'm working on a project converting older .aspx pages to newer MVC pages. I'm currently using the routing function to map the existing legacy pages to use the newer MVC pages behind the scenes. This is working great because it is preserving the existing URL, however, whenever the controller needs to redirect the user OR an MVC Url.Action link is used, the URL it sends the user to is the MVC url and not the routed legacy page url. I realize that this is how its suppose to be functioning out of the box but I was wondering if MVC has "reverse routing" functionality of some sort. By reverse routing I am looking for something that allows the Url.Action("action") to return the routed .aspx associated url with the specified action instead of the default /controller/action link.
So for instance,
here is a sample route
context.MapRoute("AboutUs", "pages/aboutus.aspx", new { controller = "default", action = "aboutus" });
This enables domain.com/pages/aboutus.aspx to run the new MVC action "aboutus" from the DefaultController behind the scenes perfectly.
However, if on another MVC page I have a link created by
Url.Action("aboutus")
it points them to domain.com/mvc/aboutus. I would like it to point them to domain.com/pages/aboutus.aspx to keep uniformity across the site.
Thanks for any insights that you can provide. I might end up having to override the Url.Action method to look up the route and return the mapped url but I just wanted to check for existing functionality so I don't reinvent the wheel.
Try using the RouteUrl extension method, which matches the route by route name rather than route parameters, like so:
Url.RouteUrl("aboutus") // route name
MSDN: http://msdn.microsoft.com/en-us/library/dd460347

Always need Https to a particular route

I have MVC3 application with SSL. I want particular page URL should always add Https. Can someone tell me how to do this. Below is the Route in Global file.
routes.MapRoute("root22",
"paybill",
new { controller = "Home", action = "PayBill" });
One more thing if my URL has https in front than should all the paths in the page also use https or not necessary?
You don't need to mess with the routing to accomplish this. Just use the [RequireHttps] attribute on your controller/actions.
[RequireHttps]
public ViewResult YourAction()
If it can, it will redirect to https. Your outgoing links don't have to use https, but when loading javascript, css, images, etc. then it should otherwise the user may get a mixed content security warning.
Above attribute works fine if you want your whole website in https after redirecting using that particular action.
But you want to secure only a single page rather than whole website, just use below code.
http://www.codehosting.net/blog/BlogEngine/post/More-fiddling-with-MVC3-and-https.aspx

Resources