我有像这样的控制器动作
[HttpGet]
public ActionResult Index(string Id)
{
}
所以实际调用就像Report / Index / {string_param_value}
我想避免使用像Report / {string_param_value}这样的索引 我在Global.asax.cs中做了以下更改
routes.MapRoute(
"Report_WithoutIndex",
"Report/{Id}",
new { controller = "Report", action = "Index" }
);
但是这个没有调用Index行动 我试过这个然后
routes.MapRoute(
name: "Index",
url: "{controller}/{id}",
defaults: new { action = "Index" },
constraints: new { action = "Index" }
);
这个对我有用但是在此之后所有其他动作都被打破了
那么调用报表控制器wuthout在url中提到索引的正确工作是什么
答案 0 :(得分:2)
关注RounteConfig.cs
,对我有用 -
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Report_WithoutIndex",
"Report/{Id}",
new { controller = "Report", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
我的控制器是 -
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
}
public class ReportController : Controller
{
public ActionResult Index(string Id)
{
return null;
}
}
当我使用/Report/2
时,我正在点击报告控制器索引操作。当我使用/Home/About
时,我开始转到Home Controller of Home controller。所有其他默认路由都按预期工作。