ASP.NET MVC - Routing - an action with file extension

后端 未结 3 1433
野的像风
野的像风 2020-11-30 07:17

is there a way to achieve calling URL http://mywebsite/myarea/mycontroller/myaction.xml This would basically \"fake\" requesting a file but the result would be

3条回答
  •  伪装坚强ぢ
    2020-11-30 08:10

    The problem is that IIS will handle the .xml file as a static file and will by default not route the XML file through your MVC application. IIS handles the request and your MVC code never gets a change to route to this file. There are a few ways around this.

    I've found the easiest way to handle this by using the IIS Rewrite module to rewrite the URL from static file URL(s) to an MVC route:

    
      
        
          
            
            
          
        
      
    
    

    Make sure you have the IIS Rewrite Module installed (separate install from the Platform Installer). If you already are using the Rewrite handler this is the most efficient solution.

    As pointed out above by Ben Foster and Jon Galloway's post, you can also map the TransferRequestHandler at your specific path you want to route. For compactness here's what you need to add to your web.config:

    
      
         
      
    
    

    You can then use an Attribute Route to handle .xml file Urls. For example:

    [Route("blog/wlwmanifest.xml")]
    public ActionResult LiveWriterManifest() {... }
    

    More info in this blog post: http://weblog.west-wind.com/posts/2015/Nov/13/Serving-URLs-with-File-Extensions-in-an-ASPNET-MVC-Application

提交回复
热议问题