5

I am using this action-link to send a route value id to controller but my id value like 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 browser understand this as a url that ends with config.xml

like this

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

and doesn't go to the controller it returns 404 - not found. How to prevent this from happening and make this config.xml as a parameter not as a file?

here is my route also

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

also i tried instead id ,filename but nothing changed

and here is my controller

[HttpGet]
        public ActionResult DeleteFile(string id)
        {
          return view("DeleteFile");
         }
Younis Qadir
  • 315
  • 1
  • 4
  • 16
  • here is my action-result [HttpGet] public ActionResult DeleteFile(string id) { return view("DeleteFile"); } – Younis Qadir Jan 08 '15 at 12:00
  • And how does your route look like? Maybe there is a "int"-contraint on it? – Christoph Fink Jan 08 '15 at 12:04
  • here is my route routes.MapRoute( name: "delete files", url: "Files/DeleteFile/{id}", defaults: new { controller = "Files", action = "DeleteFile", id= UrlParameter.Optional } ); – Younis Qadir Jan 08 '15 at 12:08

2 Answers2

3

You can do it using this overload of Html.ActionLink() like this:

@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 Fiddle

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
0

Here i find the answer we can simply add / at the end of the config.xml

here is some answers

Community
  • 1
  • 1
Younis Qadir
  • 315
  • 1
  • 4
  • 16