设置默认路由URL和属性路由?如何

时间:2017-08-06 16:33:29

标签: asp.net asp.net-mvc routing asp.net-mvc-routing asp.net-mvc-5

我的RouteConfig文件是

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

我的控制器是

 [Route("EMS/{Employee}")]
    public ActionResult Index()
    {
        return View();
    }

我的工作网址是 http://localhost:6628/EMS/Employee

但我想使用简单的http://localhost:6628作为我的默认网址,因为没有MapMvcAttributeRoutes()它工作正常

我如何在同一个项目中使用它们,例如默认控制器操作必须是员工和索引以及点击路径网址EMS /员工这样工作

  <td>
    <input type="button" id="ROUTE" value="ROUTE"   onclick="location.href='@Url.Action("Employee", "EMS")'" class="btn-info" />
                        </td>

1 个答案:

答案 0 :(得分:1)

例如,如果控制器是EmployeeController

public class EmployeeController {    
    [HttpGet]
    [Route("")] //Matches GET /
    [Route("EMS/Employee")] //Matches GET EMS/EMployee
    public ActionResult Index() {
        return View();
    }
}

您可以在操作中使用多个路线。

相关问题