路由删除图像/js/css等
我已经此路由开始工作。它是 Dot Net 4.0 System.Web.Routing。
但问题是文档中的所有路径都不再有效。如果我希望页面同时作为
www.website.com/agents/Agent Name
“真实”地址
www.website.com/portfolio.aspx?aid=123
工作,我该怎么办?当然,我可以通过使用绝对 URL 来使其工作,
<img src="http://www.website.com/images/image.png" alt="" />
但这是要走的路吗?
其实我自己也找到了答案:
routes.Add("AgentFolderGraphicsRoute", new 路线(“代理/图形/{文件夹}/{文件名}。{ext}”,新 ImageRouteHandler()));
public class ImageRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string folder = requestContext.RouteData.Values["folder"] as string;
string filename = requestContext.RouteData.Values["filename"] as string;
string ext = requestContext.RouteData.Values["ext"] as string;
if (string.IsNullOrEmpty(filename))
{
requestContext.HttpContext.Response.Clear();
requestContext.HttpContext.Response.StatusCode = 404;
requestContext.HttpContext.Response.End();
}
else
{
requestContext.HttpContext.Response.Clear();
requestContext.HttpContext.Response.ContentType = GetContentType(requestContext.HttpContext.Request.Url.ToString());
// find physical path to image here.
string filepath;
if (folder != null) filepath = requestContext.HttpContext.Server.MapPath("~/graphics/" + folder + "/" + filename + "." + ext);
else filepath = requestContext.HttpContext.Server.MapPath("~/graphics/" + filename + "." + ext);
requestContext.HttpContext.Response.WriteFile(filepath);
requestContext.HttpContext.Response.End();
}
return null;
}
private static string GetContentType(String path)
{
switch (System.IO.Path.GetExtension(path))
{
case ".bmp": return "Image/bmp";
case ".gif": return "Image/gif";
case ".jpg": return "Image/jpeg";
case ".png": return "Image/png";
default: break;
}
return "";
}
这是此页面上的版本的稍微修改版本:
http://www.phpvs.net/2009/08/06/aspnet-mvc-how-to-route-to-images-or-other-file-types/
I have gotten this Routing to work. It is Dot Net 4.0 System.Web.Routing.
But the problem is that all paths in the document are no longer working. If I want the page to work both as
www.website.com/agents/Agent Name
And the "real" address
www.website.com/portfolio.aspx?aid=123
What do I do? Of course I can make it work by using absolute URLs like
<img src="http://www.website.com/images/image.png" alt="" />
But is that the way to go?
I actually found the answer myself:
routes.Add("AgentFolderGraphicsRoute", new
Route("agents/graphics/{folder}/{filename}.{ext}", new
ImageRouteHandler()));
public class ImageRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string folder = requestContext.RouteData.Values["folder"] as string;
string filename = requestContext.RouteData.Values["filename"] as string;
string ext = requestContext.RouteData.Values["ext"] as string;
if (string.IsNullOrEmpty(filename))
{
requestContext.HttpContext.Response.Clear();
requestContext.HttpContext.Response.StatusCode = 404;
requestContext.HttpContext.Response.End();
}
else
{
requestContext.HttpContext.Response.Clear();
requestContext.HttpContext.Response.ContentType = GetContentType(requestContext.HttpContext.Request.Url.ToString());
// find physical path to image here.
string filepath;
if (folder != null) filepath = requestContext.HttpContext.Server.MapPath("~/graphics/" + folder + "/" + filename + "." + ext);
else filepath = requestContext.HttpContext.Server.MapPath("~/graphics/" + filename + "." + ext);
requestContext.HttpContext.Response.WriteFile(filepath);
requestContext.HttpContext.Response.End();
}
return null;
}
private static string GetContentType(String path)
{
switch (System.IO.Path.GetExtension(path))
{
case ".bmp": return "Image/bmp";
case ".gif": return "Image/gif";
case ".jpg": return "Image/jpeg";
case ".png": return "Image/png";
default: break;
}
return "";
}
Which is a slightly modified version of the one on this page:
http://www.phpvs.net/2009/08/06/aspnet-mvc-how-to-route-to-images-or-other-file-types/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保
RegisterRoutes()
方法中的第一件事是:这将指示路由引擎忽略对资源(如图像或文件)的请求。
Make sure the first thing in your
RegisterRoutes()
method is:This will instruct the routing engine to ignore requests for resources (like images or files).