2013-01-23 17 views
1

我正在与ASP.NET MVC 4一起工作,我正在尝试编写一个非常基本的路线,但它不起作用,让它非常沮丧!设置一个简单的ASP.NET MVC路线

我想要的网址http://www.mywebsite.com/my-page触发称为控制器和操作方法指数

我没有其他途径设置除了这一点:

RouteTable.Routes.MapRoute(
    name: "Default", 
    url: "my-page", 
    defaults: new { controller = "Page", action = "Index" } 
); 

是不是有什么不正确用我的设置或者我要去哪里错了?

我得到的错误是:

控制器的路径“/我的页面”未找到或没有实现一个IController。

+0

这工作正常从默认的Mvc网站。你能告诉我们整个RouteConfig吗? –

+0

你的路线看起来不错。添加你的控制器类到你的文章。 –

回答

0

主要问题是您试图覆盖默认路由。在MVC4中,路由在App_Start/RouteConfig.cs中定义。 “默认”的路线应该是最后的路线:

 routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 

然后,您的具体路线,使用默认路由前执行以下操作:

 routes.MapRoute(
      name: "MyPage", 
      url: "my-page", 
      defaults: new { controller = "Page", action = "Index" } 
     ); 

Fianlly,确保你有一个控制器PageController.cs与一个行动索引和视图视图/页/ Index.cshtml:

public class PageController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(); 
    } 
}