C# ASP.NET MVC2 路由通用处理程序

发布于 2024-09-30 02:00:31 字数 279 浏览 3 评论 0原文

也许我正在寻找错误的东西或试图以错误的方式实现这一点。我使用通用处理程序动态生成图像。我目前可以使用以下方式访问我的处理程序:

ImageHandler.ashx?width=x&height=y

我更愿意使用类似这样的方式访问我的处理程序:

images/width/height/imagehandler

这可能吗?我在 google 上找到的几个示例不适用于 MVC2。

干杯。

Maybe I am searching the wrong thing or trying to implement this the wrong way. I am dynamically generating an image using a Generic Handler. I can currently access my handler using:

ImageHandler.ashx?width=x&height=y

I would much rather access my handler using something like

images/width/height/imagehandler

Is this possible the few examples I found on google didn't work with MVC2.

Cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

木緿 2024-10-07 02:00:31

昨晚我继续研究这个问题,令我惊讶的是,我更接近我想到的解决方案。对于将来可能遇到此问题的任何人,这里是我如何实现 MVC2 路由到通用处理程序的方法。

首先,我创建了一个继承 IRouteHandler 的类

public class ImageHandlerRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        var handler = new ImageHandler();
        handler.ProcessRequest(requestContext);

        return handler;
    }
}

,然后实现了通用处理程序,创建了 MVC 友好的 ProcessRequest。

public void ProcessRequest(RequestContext requestContext)
{
    var response = requestContext.HttpContext.Response;
    var request = requestContext.HttpContext.Request;

    int width = 100;
    if(requestContext.RouteData.Values["width"] != null)
    {
        width = int.Parse(requestContext.RouteData.Values["width"].ToString());
    }

    ...

    response.ContentType = "image/png";
    response.BinaryWrite(buffer);
    response.Flush();
}

然后添加到 global.asax 的路由

RouteTable.Routes.Add(
    new Route(
        "images/{width}/{height}/imagehandler.png", 
        new ImageShadowRouteHandler()
    )
 );

,然后您可以使用

<img src="/images/100/140/imagehandler.png" />

我使用通用处理程序在需要时生成动态水印来调用您的处理程序。希望这可以帮助其他人。

如果您有任何疑问,请告诉我,我会尽力帮助您。

I continued working on this problem last night and to my surprise I was closer to the solution that I had thought. For anyone who may struggle with this in the future here is how I implemented MVC2 Routing to a Generic Handler.

First I created a class that inherited IRouteHandler

public class ImageHandlerRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        var handler = new ImageHandler();
        handler.ProcessRequest(requestContext);

        return handler;
    }
}

I then implemented the generic handler creating an MVC friendly ProcessRequest.

public void ProcessRequest(RequestContext requestContext)
{
    var response = requestContext.HttpContext.Response;
    var request = requestContext.HttpContext.Request;

    int width = 100;
    if(requestContext.RouteData.Values["width"] != null)
    {
        width = int.Parse(requestContext.RouteData.Values["width"].ToString());
    }

    ...

    response.ContentType = "image/png";
    response.BinaryWrite(buffer);
    response.Flush();
}

Then added a route to the global.asax

RouteTable.Routes.Add(
    new Route(
        "images/{width}/{height}/imagehandler.png", 
        new ImageShadowRouteHandler()
    )
 );

then you can call your handler using

<img src="/images/100/140/imagehandler.png" />

I used the generic handler to generate dynamic watermarks when required. Hopefully this helps others out.

If you have any questions let me know and I will try and help you where possible.

傲影 2024-10-07 02:00:31

我已经使用该解决方案很长时间了,您可以将其设为通用,以便它将接受您将来拥有的任何处理程序:

internal class RouteGenericHandler<T> : IRouteHandler where T : IHttpHandler, new()
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new T();
    }
}

在 RegisterRoutes 方法上:

routes.Add(new Route("Name", new RouteGenericHandler<TestHandler>()));

I use that solution for a long time now, you can make it generic so it will accept any handler you'll have in the future:

internal class RouteGenericHandler<T> : IRouteHandler where T : IHttpHandler, new()
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new T();
    }
}

And on RegisterRoutes method:

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