HTTP模块只在特定的MVC路线 [英] HttpModule only on specific MVC route

查看:87
本文介绍了HTTP模块只在特定的MVC路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的 IHttpModule的,我想只有一个特定的路线上工作。

例如: http://example.com/HandleAzureTask

我想这个模块只能调用/的 / HandleAzureTask 路线处理。

由于这不是一个控制器,我真的不能在其上设置 [授权] 属性;我怎么能强迫它只能被调用/处理,如果用户进行身份验证?

我在ASP.NET MVC 4,目前已添加到我的web.config模块例如:

<模块>
  <清除NAME =AzureWebDAVModule/>
  <添加名称=AzureWebDAVModuleTYPE =VMC.WebDAV.Azure.Module.AzureWebDAVModule,VMC.WebDAV.Azure.Module/>
< /模块>


解决方案

的HttpModules被称为在每次请求(HttpHandlers的,而是可以被过滤)。如果你只是想只对选定的路线完成你的任务,你可以做到以下几点:

设置路线是这样的:

  routes.MapRoute(
    名称:AzureWebDAVRoute
    网址:HandleAzureTask
    //注意enableHandler参数
    默认:新{控制器=YourController,行动=YourAction,enableHandler =真}
);

在你的模块:

 公共类AzureWebDAVModule:IHttpModule的
{
    公共无效初始化(HttpApplication的情况下)
    {
        //你不能直接在这里访问请求的对象,它会抛出一个异常
        context.PostAcquireRequestState + =新的EventHandler(context_PostAcquireRequestState);
    }    无效context_PostAcquireRequestState(对象发件人,EventArgs的发送)
    {
        HttpApplication的背景下=(HttpApplication的)寄件人;
        的RouteData的RouteData = context.Request.RequestContext.RouteData;        如果(的RouteData = NULL&放大器;!&安培; routeData.Values​​ [enableHandler]!= NULL)
        {
            //做你的东西
        }
    }    公共无效的Dispose()
    {
        //
    }
}

现在你的任务将只选定的路线上进行。请注意,您所需要的参数,因为你找不到的名字当前路由。

希望它帮助!

卢卡

I have a custom IHttpModule that I would like to only work on a specific route.

For example : http://example.com/HandleAzureTask

I want this module to only be invoked/handled on the /HandleAzureTask route.

Since this is not a controller, I can't really set the [Authorize] attribute on it; how can I force it only to be invoked/handled if user is authenticated?

I am on ASP.NET MVC 4 and currently have my module added to web.config as such:

<modules>
  <remove name="AzureWebDAVModule" />
  <add name="AzureWebDAVModule" type="VMC.WebDAV.Azure.Module.AzureWebDAVModule, VMC.WebDAV.Azure.Module" />
</modules>

解决方案

HttpModules are called on every request (HttpHandlers, instead, can be filtered). If you just want to perform your task only on the selected route, you can do the following:

Set up a route like this:

routes.MapRoute(
    name: "AzureWebDAVRoute",
    url: "HandleAzureTask",
    // notice the enableHandler parameter
    defaults: new { controller = "YourController", action = "YourAction", enableHandler = true }
);

On your module:

public class AzureWebDAVModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        // you can't directly access Request object here, it will throw an exception
        context.PostAcquireRequestState += new EventHandler(context_PostAcquireRequestState);
    }

    void context_PostAcquireRequestState(object sender, EventArgs e)
    {
        HttpApplication context = (HttpApplication)sender;
        RouteData routeData = context.Request.RequestContext.RouteData;

        if (routeData != null && routeData.Values["enableHandler"] != null)
        {
            // do your stuff
        }
    }

    public void Dispose()
    {
        //
    }
}

Now your task will be performed on the selected route only. Please note that you need the parameter since you can't find the current route by name.

Hope it helps!

Luca

这篇关于HTTP模块只在特定的MVC路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆