Question

Additional:

Download my project with my failed attempt at converting to AttributeRouting.
The project will run correctly when the message on the home page changes between "No new email." and "You have mail!". In its current errored state that message does not change.
Errors in the javascript console will show.
Navigating directly to /Checkemail with the browser results in 404 error.

Original Post:

This is a question about AttributeRouting (using latest v3.4.1).

I am trying to hook a GET[""] onto the following code.

[GET("")] I get a 404 - resource not found.
[GET("CheckEmail")] I get a 405 - Method not allowed.

I am trying to convert this project to AttributeRouting: source code. The checkemail action is where I am failing.

The method is an asynchronous method as part of an "ajax long polling" technique.

I have the following with my dismal attempts commented:

public class CheckEmailController : AsyncController
    {
        //
        // GET: /CheckEmail/

        //tried [GET("")]
        //tried [GET("CheckEmail")]
        public void IndexAsync()
        {
            AsyncManager.OutstandingOperations.Increment();
            MyAsyncEmailChecker.CheckForEmailAsync(hasEmail =>
            {
                AsyncManager.Parameters["hasEmail"] = hasEmail;
                AsyncManager.OutstandingOperations.Decrement();
            });
        }

        private class IndexResponse
        {
            public bool d { get; set; }
        }

        public JsonResult IndexCompleted(bool hasEmail)
        {
            return this.Json(new IndexResponse() { d = hasEmail });
        }

    }

Global.asax.cs - as for all my AR projects

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }
}

thank you

Was it helpful?

Solution

If you are on ASP.NET MVC 4 and .NET 4.5 then you should just use the async keyword and Tasks. This should fix your issue with routing and reduce the complexity in your controllers. Here is the a link to whitepaper by microsoft.

http://www.asp.net/mvc/tutorials/mvc-4/using-asynchronous-methods-in-aspnet-mvc-4

but to sum it up your code would change to this.

public class CheckEmailController : AsyncController
{
    //
    // GET: /CheckEmail/
    [GET("CheckEmail")]
    public async Task<ActionResult> Index()
    {
      return View( new IndexReponse {
        d = await MyAsyncEmailChecker.CheckForEmailAsync() 
      });
    }

    public class IndexResponse
    {
        public bool d { get; set; }
    }


}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top