【问题标题】:How to replace string or hide string in url after mapping to controller from view?从视图映射到控制器后如何替换字符串或隐藏 url 中的字符串?
【发布时间】:2019-08-29 08:05:41
【问题描述】:

在从视图映射到控制器后,我试图隐藏或替换 URL 中的字符串。

查看

    <a href='@Url.Action("Product", "Index", new { prodID =@item.ProductID 
    })'>

控制器

      public ActionResult Product()
      {

        return View(model);
      }

现在我在这样的网址中得到这个http://localhost:9210/Index/Product?prodID=1 但我想要url 像这样http://localhost:9210/Index/Product/1 那么我该怎么做呢?请帮忙

【问题讨论】:

  • 那么您必须在路由配置中或通过属性定义路由/Product/{prodID}
  • 是的,没错,我想要这样的`localhost:9210/Index/Product/1
  • @CodeCaster 我该怎么做请告诉我
  • @icon 试试下面的答案。

标签: c# asp.net asp.net-mvc asp.net-mvc-4 routes


【解决方案1】:

你必须这样通过。

<a href='@Url.Action("Product", "Index", new { id =@item.ProductID 
})'>

可能是因为route.config 文件中定义的参数名称为id 而不是prodID

【讨论】:

    【解决方案2】:

    您可以使用Route 属性指定预期的路线。

    [Route("Index/Product/{prodId}")]
    public IHttpActionResult UpdateStatus(int prodId)
    {
    }
    

    【讨论】:

    • 它显示红线吹[Route我在动作结果上方添加了路线线` [Route("Index/Product/{prodID}")] public ActionResult Product(int prodID)`
    猜你喜欢
    • 2012-04-26
    • 1970-01-01
    • 2021-09-23
    • 2013-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多