2009-01-15 68 views
1

在的RegisterRoutes方法我的Global.asax.cs文件,我把爲什麼ASP.NET MVC URL路由無法找到我的控制器?

routes.MapRoute("messages", 
    "Message/{id}", 
    new { controller = "Archive", action = "Message", id = 0 }); 

然後我創造了這個控制器:

namespace TestingApp.Controllers 
{ 
    public class ArchiveController : Controller 
    { 
     public string Message(int id) 
     { 
      return "testing: you will receive the message: " + id.ToString(); 
     } 
    } 
} 

但在我browsser當我去:

http://.../Message/34 

我得到了404個。

我還需要定義什麼,以便路由找到我的控制器?

回答

4

嘗試默認一個之前定義你的具體路線:

routes.MapRoute(
    "messages", 
    "Message/{id}", 
    new { controller = "Archive", action = "Message", id = 0 }); 

routes.MapRoute(
    "Default", 
    "{controller}/{action}/{id}", 
    new { controller = "Home", action = "Index", id = "" }); 
1

我覺得你的留言方法應該返回一個ActionResult實例:

public ActionResult Message(int id) 
{ 
    return new ContentResult { 
     Content = "testing: you will receive the message: " + id.ToString() 
    }; 
} 
+0

返回一個字符串控制器是可以接受的ASP.NET- MVC – AnthonyWJones 2009-01-15 11:35:56