Вопрос

Может ли кто -нибудь, пожалуйста, помочь мне с этим, поскольку я понятия не имею, почему публичный ihttphandler Gethttphandler (requestContext requestContext) не выполняется. В моем Global.asax.cs у меня есть

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

        routes.Add("ImageRoutes", new Route("Images/{filename}", new CustomRouteHandler()));

    }

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
    }
}

// CustomRouteHandler реализация ниже

public class CustomRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        // IF I SET A BREAK POINT HERE IT DOES NOT HIT FOR SOME REASON.  
        string filename = requestContext.RouteData.Values["filename"] as string;

        if (string.IsNullOrEmpty(filename))
        {
            // return a 404 HttpHandler here 
        }
        else
        {
            requestContext.HttpContext.Response.Clear();
            requestContext.HttpContext.Response.ContentType = GetContentType(requestContext.HttpContext.Request.Url.ToString());

            // find physical path to image here.   
            string filepath = requestContext.HttpContext.Server.MapPath("~/logo.jpg");

            requestContext.HttpContext.Response.WriteFile(filepath);
            requestContext.HttpContext.Response.End();

        }
        return null;
    }
}

Может ли любое тело сказать мне, что мне здесь не хватает. Просто публичный ihttphandler gethttphandler (requestContext requestContext) не стреляет.

Я тоже ничего не меняю в Web.Config. Чего мне здесь не хватает? Пожалуйста помоги.

Это было полезно?

Решение

routes.MapRoute(
    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

routes.Add("ImageRoutes", new Route("Images/{filename}", new CustomRouteHandler()));

Вам нужно перевернуть эти два заявления. {controller}/{action}/{id} Вероятно, соответствует входящему URL, поэтому вам нужно объявить свое специальное Images/{FileName} перед этим.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top