【问题标题】:Routing removes images/js/css etc路由删除图像/js/css 等
【发布时间】:2011-10-25 05:20:44
【问题描述】:

我已经让this Routing 工作了。它是 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 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 "";
        }

这是此页面上的稍作修改的版本:

http://www.phpvs.net/2009/08/06/aspnet-mvc-how-to-route-to-images-or-other-file-types/

【问题讨论】:

    标签: c# asp.net routing path


    【解决方案1】:

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

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

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

    【讨论】:

    • 我不认为,我可以在非 MVC 应用程序中做到这一点。
    • MVC 实际上只是一个 ASP.NET 扩展。 MVC 中的路由引擎与 WebForms 中使用的相同,因此它应该可以工作。你试过了吗?
    • 我不能,因为 System.Web.Routing.RouteCollection 不包含 IgnoreRoute
    • 您必须使用 .NET 3.5 .dll。 4.0 中的RouteCollection 类确实实现了IgnoreRoute() 方法。不过,很高兴你找到了一些有用的东西。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 2013-02-01
    相关资源
    最近更新 更多