Asp.net mvc 为什么ASP.NET MVC URL路由找不到我的控制器?

Asp.net mvc 为什么ASP.NET MVC URL路由找不到我的控制器?,asp.net-mvc,routing,Asp.net Mvc,Routing,在registerOutes方法中的Global.asax.cs文件中,我将 routes.MapRoute("messages", "Message/{id}", new { controller = "Archive", action = "Message", id = 0 }); 然后我创建了这个控制器: namespace TestingApp.Controllers { public class ArchiveController : Controller

在registerOutes方法中的Global.asax.cs文件中,我将

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();
        }
    }
}
但在我的浏览器中,当我去:

http://.../Message/34
我有一个404


我还需要定义什么才能使路由找到我的控制器?

尝试在默认路由之前定义您的特定路由:

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

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

尝试在默认路由之前定义您的特定路由:

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

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

我认为您的Message方法应该返回ActionResult实例:

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

我认为您的Message方法应该返回ActionResult实例:

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