2013-01-21 49 views
0

附加:如何使用AttributeRouting将路由添加到/ Checkemail异步方法?

Download我的项目与转换成AttributeRouting我的失败尝试。
当主页上的消息在“无新邮件”之间更改时,项目将正常运行。和“你有邮件!”。在目前的错误状态下,消息不会改变。
在JavaScript控制台中的错误将显示。
使用浏览器直接导航到/ Checkemail导致404错误。

原贴:

这是一个问题关于AttributeRouting(使用最新的V3.4.1)。

我试图挂钩GET[""]到以下代码。

[GET("")]我得到一个404 - 找不到资源。
[GET("CheckEmail")]我得到405 - 方法不允许。

我想将此项目转换为AttributeRouting:source code。 checkemail行动是我失败的地方。

该方法是一种异步方法,作为“ajax long polling”技术的一部分。

我已经在我的惨淡尝试以下评论:

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 - 为我所有的AR项目

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); 
    } 
} 

谢谢

+0

您可以升级到ASP.NET MVC 4和.Net 4.5。此时你不再需要使用旧模式。另外,你的电子邮件检查器是做什么的?它是本地还是远程? –

回答

2

如果您在ASP.NET MVC 4和.NET 4.5,那么你应该只使用async关键字和任务。这应该解决您的路由问题并降低控制器的复杂性。这里是微软白皮书的链接。

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

,但概括起来讲你的代码会改变这一点。

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; } 
    } 


} 
相关问题