【问题标题】:C# MVC dont let user key in index in the urlC# MVC 不允许用户在 url 中键入索引
【发布时间】:2017-04-16 20:34:35
【问题描述】:

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

如何禁用 http://example.com/controller/index 这样链接就会重定向到http://example.com/controller

【问题讨论】:

  • 什么意思禁用呢?用户可以在地址栏中输入任何他们想要的内容。
  • 这样链接会重定向到example.com/controller而不是url是example.com/controller/index
  • 假设您使用默认路由,../controller/index../controller 都将转到命名控制器的 Index() 方法。如果用户愿意,您无法阻止用户在地址栏中输入../controller/index
  • 我希望通过在 route.map() 或任何其他方法上配置某些内容来将 ../controller/index 重定向到 ../controller。
  • 您需要在 IIS 中使用配置 URL 重写

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


【解决方案1】:

可能路由上没有配置,我要按照下面的方法来做。

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


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-15
    • 2014-10-28
    • 2011-07-30
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多