静态路由文件在ASP.NET MVC [英] Static File Routes in ASP.NET MVC

查看:169
本文介绍了静态路由文件在ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个ASP.NET MVC应用程序。在这个程序,我需要动态生成网站地图时,其请求。我知道如何在普通配置的路由。但是,我不知道我是否可以创建一个特定的文件的路径。目前,我已经中RouteConfig.cs以下内容:

I'm working on an ASP.NET MVC app. In this app, I need to dynamically generate the sitemap when its requested. I know how to configure routes in general. However, I'm not sure if I can create a route for a specific file. Currently, I have the following in RouteConfig.cs:

routes.MapRoute(
  name: "Sitemap",
  url: "resources/sitemap.xml",
  defaults: new { controller = "Site", action = "Sitemap" }
);

在我SiteController,我有以下几点:

In my SiteController, I have the following:

public ActionResult Sitemap()
{
  // I will build my sitemap.xml file here and return it.
}

当我进入/resources/sitmap.xml到浏览器的地址栏中,我发现我的网站地图()动作永远不会被绊倒。它甚至有可能在ASP.NET MVC中设置特定文件的路径?如果是这样,怎么样?

When I enter /resources/sitmap.xml into the browser's address bar, I noticed that my Sitemap() action never gets tripped. Is it even possible in ASP.NET MVC to setup a route for a specific file? If so, how?

谢谢,

推荐答案

所以,你有一些步骤做 -

So you got to do in some steps -

第1步 - 地图的XML扩展由净路由照顾
添加以下下的Web.config节< system.webServer> -

Step 1 - Map the xml extension to take care of by .Net for routing Add following section in Web.config under <system.webServer> -

<handlers>
  <add name="HtmlFileHandler" path="*.xml" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>

第2步 - 定义你的路由,并覆盖匹配现有文件的请求

Step 2 - Define your routes and override requests that match an existing file.

routes.RouteExistingFiles = true;

routes.MapRoute(
   name: "Sitemap",
   url: "{site}.xml",
   defaults: new { controller = "Site", action = "Sitemap", site = UrlParameter.Optional }
 );

第3步 - 尝试访问 /SiteMap.xml ,你会得到控制动作命中

then step 3 - Try to access /SiteMap.xml, you will get Controller action hit.

这篇关于静态路由文件在ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆