路由删除图像/js/css等

发布于 2024-12-12 04:16:10 字数 2731 浏览 1 评论 0原文

我已经此路由开始工作。它是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭⌒浅淡时光〆 2024-12-19 04:16:10

确保 RegisterRoutes() 方法中的第一件事是:

routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" );

这将指示路由引擎忽略对资源(如图像或文件)的请求。

Make sure the first thing in your RegisterRoutes() method is:

routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" );

This will instruct the routing engine to ignore requests for resources (like images or files).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文