C#MVC不要让用户键入url中的索引

时间:2016-12-02 02:05:48

标签: asp.net-mvc asp.net-mvc-4 url routes

允许用户键入URL http://example.com/controller获取操作“索引”

如何禁用 http://example.com/controller/index 以便链接重定向到http://example.com/controller

1 个答案:

答案 0 :(得分:0)

Maybe don't have configuration on the route , I have to do with the method below.

public ActionResult Index(){
      string item = Url.Content(Request.Url.PathAndQuery);


      if (item.Equals("/controller/index", StringComparison.InvariantCultureIgnoreCase))
      {
         return Redirect("/controller");
      }else
      {
           return View();
      }
}