سؤال

يمكن لأي شخص ما الرجاء مساعدتي في ذلك لأنني ليس لدي أي فكرة لماذا لا يتم تنفيذ Ihttphandler Gethttphandler (requestcontextextext). في بلدي 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 (requestcontextestextextext) لا يطلق النار.

لا أغير أي شيء في 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 الوارد، لذلك تحتاج إلى إعلان خاص بك صور / {filename} قبل ذلك.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top