包含特定字符的 ActionLink 路由值

ActionLink route values containing specific characters

我正在使用此操作-link 将路由值 id 发送到控制器,但我的 ID 值是这样的 config.xml 这是我的行动-link

 @Html.ActionLink("Destroy", "DeleteFile", "Files", new { id = "config.xml"})

问题是当我想点击这个 link 浏览器将其理解为以 config.xml

结尾的 url

像这样

http://localhost:12380/Files/DeleteFile/config.xml

并且不去控制器 returns 404 - not found。 如何防止这种情况发生并使此 config.xml 作为参数而不是文件?

这也是我的路线

routes.MapRoute(
              name: "delete files",
              url: "Files/DeleteFile/{id}",
              defaults: new
              {
                  controller = "Files",
                  action = "DeleteFile",
                  id= UrlParameter.Optional
              }
            );

我也尝试过 id ,filename 但没有任何改变

这是我的控制器

[HttpGet]
        public ActionResult DeleteFile(string id)
        {
          return view("DeleteFile");
         }

您可以像这样使用 this overload of Html.ActionLink() 来做到这一点:

@Html.ActionLink("Destroy", 
                "DeleteFile", 
                "Files", 
                new RouteValueDictionary { {"file", Url.Encode("config.xml")} }, 
                null)

和行动:

public ActionResult DeleteFile(string file)
{
  // delete logic here
  return View();
}

来自MSDN

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName,
    RouteValueDictionary routeValues,
    IDictionary<string, Object> htmlAttributes
)

Here is a working DEMO Fiddle

在这里我找到了答案我们可以简单地在 config.xml

的末尾添加 /

here is some answers