ActionLink route values ​​containing specific characters

I use this action link to send the id route value to the controller, but my id value is similar to this config.xml and here is my action-link

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

The question is when I want to click this link with a browser, we understand it as a url that ends with config.xml

like this

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

and does not go to the controller, it returns 404 - not found . How to prevent this and make this config.xml as a parameter not as a file?

here my route is also

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

I also tried instead of id ,filename , but nothing has changed

and here is my controller

 [HttpGet] public ActionResult DeleteFile(string id) { return view("DeleteFile"); } 
+5
source share
2 answers

Here I find the answer, we can just add / to the end of config.xml

here are some answers

0
source

You can do this with this Html.ActionLink () overload as follows:

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

and action:

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

From 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 script

+3
source

Source: https://habr.com/ru/post/1210662/


All Articles