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
Nands
  • 379
  • 3
  • 19

3 Answers3

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.

DavidG
  • 113,891
  • 12
  • 217
  • 223
  • I was able get http://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. – Nands Jun 02 '14 at 07:55
  • So was my answer correct? If so, are you asking another question? – DavidG Jun 02 '14 at 23:54
  • I had to change the code as in the above comment to make it working. – Nands Jun 03 '14 at 04:32
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

Community
  • 1
  • 1
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" />
Nands
  • 379
  • 3
  • 19