Nands
Nands

Reputation: 399

Adding filename in url

I want to customize route in ASP.NET MVC. With

@Url.Action("ViewDoc", "Home", new { FileName = "ABC.pdf" })

and

 routes.MapRoute(
         name: "",
         url: "{controller}/{action}/{FileName}",
         defaults: new
         {
             controller = "Home",
             action = "ViewDoc",
             FileName = UrlParameter.Optional
         }

I get

http://localhost/Home/ViewDoc?FileName=ABC.pdf

How to get the below?

http://localhost/Home/ViewDoc/ABC.pdf

Upvotes: 0

Views: 1695

Answers (3)

user4084148
user4084148

Reputation:

Regarding your 404 error:

I'm using the same kind of URL with a file name at the end and getting the same routing problem. Just like you, I try to catch the call with a controller.

I think the problem is the URL is treated as a direct link to a file on the server and it will just try to go get the file instead of calling the controller. Not finding the file at the physical location suggested by the URL will trigger a 404 error.

The workaround I chose to use is adding a "/" character at the very end of the URL after the file name. There are others.

I suggest you read this related question: Dots in URL causes 404 with ASP.NET mvc and IIS

Upvotes: 1

Nands
Nands

Reputation: 399

I was able get

 localhost/Home/ViewDoc/ABC.pdf 

with

public FileResult View(string FileName) { 

and

routes.MapRoute( "", "Home/ViewDoc/{FileName}", new { controller = "Home", action = "ViewDoc" } ); 

For Error 404.0 added the below under

 <add
       name="AdfsMetadata"
       path="/Home/ViewDocu/*"
       verb="POST"
       type="System.Web.Handlers.TransferRequestHandler"
       preCondition="integratedMode,runtimeVersionv4.0" />

Upvotes: 0

DavidG
DavidG

Reputation: 119206

The code you have pasted is correct but the ordering in your route setup is probably wrong. Move the routes.MapRoute method to be above the default route and it should work as expected.

Upvotes: 2

Related Questions