Asp.net URL重写static.example.com的IIS
我正在ASP.NET中开发并拥有具有以下文件夹结构的服务器应用程序:Asp.net URL重写static.example.com的IIS,asp.net,url-rewriting,iis-7.5,Asp.net,Url Rewriting,Iis 7.5,我正在ASP.NET中开发并拥有具有以下文件夹结构的服务器应用程序: WWW -> IMG -> CSS -> JS -> ASPX 例如: www1.example.com/IMG/file.png 我的目标是将所有传入的请求重写到IMG、CSS和JS的子域中。 以便: 或 将变成: static.example.com/IMG/file.png 他们应该在自己的网站上使用自己的文件夹。它应该只通过static.example.com路由。我知道我可以用A
WWW
-> IMG
-> CSS
-> JS
-> ASPX
例如:
www1.example.com/IMG/file.png
我的目标是将所有传入的请求重写到IMG、CSS和JS的子域中。
以便:
或
将变成:
static.example.com/IMG/file.png
他们应该在自己的网站上使用自己的文件夹。它应该只通过static.example.com路由。我知道我可以用Apache做到这一点,但到目前为止,我还没有任何使用IIS的经验
这在技术上怎么可能?我拥有IIS 7.5的全部权限。有几个应用程序具有相同的结构,我想使用每个IMG,CSS和JS文件夹。我是否有绩效提升
编辑:2014年10月28日
希望这张图片能让它更清晰一点。谢谢你的回答,这对我帮助很大!因此,我可以将我的URL重写为“static.server.com”,现在我需要来自正确来源的图像,可以是www1.server.com/img/abc.png或www2.server.com/img/abc.png。静态网站不应该有一个真正的路径,它应该是一个空白的网站,可以监听主机标题或post/get,从正确的来源提供图像、css和javascript。
提前感谢,
Daniel如本中所述,您可以使用httpmodule。因此,您可以使用同一问题中的代码,并根据您的要求对其进行修改
private void Application_BeginRequest(Object source, EventArgs e)
{
// Create HttpApplication and HttpContext objects to access
// request and response properties.
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
string filePath = context.Request.FilePath;
string fullPath = context.Request.Url.AbsoluteUri;
string fileExtension = VirtualPathUtility.GetExtension(filePath);
if (fileExtension.Equals(".gif"))
{
context.Response.ContentType = "image/gif";
context.Response.Redirect(fullPath.Replace("www.example.com", "static.example.com"));
}
}
更新
在web.config
文件中可以有类似的内容
<rewrite>
<rules>
<rule name="Forward to static file server">
<match url="^.+\.(?:jpg|bmp|gif)$" />
<action type="Rewrite" url="http://static_file_server/{R:0}" />
</rule>
</rules>
</rewrite>
我不知道您的实际用例是什么(或者可能误解了您的问题),所以这是一个“简单/r”选项:
- DNS(无论如何,您都需要它)和李>
- 是,如果您共享IP,则为主机头
- 更多请求(重定向)
- 为应用程序添加更多任务(检查每个请求的httpmodule)
Hth…是否也可以通过Web.conf执行此操作?我不想在代码方面这样做,因为它应该是可托管的,并且可以作为独立软件运行。我这么晚才回复你。。。我的用例是,我在外部有多个应用程序,它们使用相同的CSS、JS和图像。为了提高性能,我想通过子域共享这些文件。
private void Application_BeginRequest(Object source, EventArgs e)
{
// Create HttpApplication and HttpContext objects to access
// request and response properties.
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
string filePath = context.Request.FilePath;
string fullPath = context.Request.Url.AbsoluteUri;
string fileExtension = VirtualPathUtility.GetExtension(filePath);
if (fileExtension.Equals(".gif"))
{
context.Response.ContentType = "image/gif";
context.Response.Redirect(fullPath.Replace("www.example.com", "static.example.com"));
}
}
<rewrite>
<rules>
<rule name="Forward to static file server">
<match url="^.+\.(?:jpg|bmp|gif)$" />
<action type="Rewrite" url="http://static_file_server/{R:0}" />
</rule>
</rules>
</rewrite>