【问题标题】:Why can't ASP.NET MVC URL routing find my controller?为什么 ASP.NET MVC URL 路由找不到我的控制器?
【发布时间】:2009-01-15 09:43:00
【问题描述】:

在我的 Global.asax.cs 文件的 RegisterRoutes 方法中,我放了

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。

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

【问题讨论】:

    标签: asp.net-mvc routing


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      我认为你的 Message 方法应该返回一个 ActionResult 实例:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-21
        • 1970-01-01
        • 2013-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-31
        • 1970-01-01
        相关资源
        最近更新 更多