0

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
0

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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" } ); But now I get error: HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
So was my answer correct? If so, are you asking another question?
I had to change the code as in the above comment to make it working.
1

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

Comments

0

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" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.