Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net .NET MVC 4.5重写索引路由_Asp.net_Asp.net Mvc Routing - Fatal编程技术网

Asp.net .NET MVC 4.5重写索引路由

Asp.net .NET MVC 4.5重写索引路由,asp.net,asp.net-mvc-routing,Asp.net,Asp.net Mvc Routing,目前我有这样一个控制器: class AccountController : Controller { public ActionResult Index() { return View(); } } 除了用户进入此页面时将具有以下url的一部分外,一切正常: http://myhost/account/index,这是不需要的。我怎样才能拥有http://myhost/account只有这样吗?像这样的东西可以达到目的 routes.MapRoute(

目前我有这样一个控制器:

class AccountController : Controller {

    public ActionResult Index() {
        return View();
    }
}
除了用户进入此页面时将具有以下url的一部分外,一切正常:
http://myhost/account/index
,这是不需要的。我怎样才能拥有
http://myhost/account
只有这样吗?

像这样的东西可以达到目的

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

这也可以通过属性路由装饰来完成,使用
[Route]
您想要的默认方法。

您的解决方案与mvc兼容吗?我尝试了
[Route]
,但它给了我404:
[Route(“”)public ActionResult Index(){return View();}
我想这可能取决于您是否已经使用了属性路由。您当前如何在应用程序上定义路由?它遵循
{controller}/{action}
的约定。我没有改变任何事情。使用拥有的启动类,但在启动类构造函数中,唯一调用的方法是MapSignalR()。这意味着
myhost/account/index
将映射到AccountController->index实例方法。控制器的其余部分只接受post请求。这是因为应用程序分为几个部分,每个部分都是一个中等大小的SPA,您只关心您所在的控制器,而不关心操作。@bigopon:将我写的路线添加到路线定义的最顶端,这样它会首先为新手缺乏知识感到抱歉,这是对MVC的建议吗?因为我很难找到在中配置路由的方法。如果是,你能分享一些代码如何配置请。