Showing posts from May, 2014

HTTP Error 404 on WebAPI 2 Routes - Fix

ASP.NET Web API 2 Rocks.

I was using built in Attribute Routing and everything else worked fine But, I faced a crazy error 404 on my routes when I was declaring this route.

[RoutePrefix("api/lookups")] public class LookupsApiController : ApiControllerBase {         [Route("")]         [HttpGet]         public HttpResponseMessage Get()         {             return Request.Success(1);         } }
I was going crazy over this error. I Installed "WebAPI 2 Help Page" [Install-Package Microsoft.AspNet.WebApi.HelpPage] and Tracing [Install-Package Microsoft.AspNet.WebApi.Tracing], the routes are registered and shows in the help page.

But it wasn't working.

And then found this answer in SO. (
It says that I was using conventional routes. "note that requests matching conventional routes can never reach attributed controller/actions."

My fix?
change the route prefix like this.


Automatic redirect upon session timeout using ASP.NET MVC and Javascript

Recently (Not so long ago, more like 10 mins ago) my asked me to automatically redirect the user to the login screen after session is expired. I googled it, found 2 ways. One works well with NON-AJAX websites and it is the simplest.

 <meta http-equiv="refresh" content="@FormsAuthentication.Timeout.TotalSeconds;url=@FormsAuthentication.LoginUrl" />

If you are making a site like facebook, then using the above method would cause number of users to decrease from 1 billion to 0. Why? because this method counts only full page refreshes, not ajax requests.

So, next method is using Javascript and thanks to this blog. automatic-redirect-upon-session-timeout-using-javascript/

Following is the same logic with MVC Forms authentication specific Razor code.

First, the web.config Forms Auth entries.

<system.web>    <authentication mode="Forms">       <forms loginUrl="~/Login" slidingExpiration="true" timeout="20" /> …