加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 为什么httphandler没有运行

发布时间:2020-12-16 03:39:40 所属栏目:asp.Net 来源:网络整理
导读:我为ASP.NET MVC4站点编写了一个生成图像的httpHandler.不调用ProcessRequest函数.有什么想法吗? MVC4,IIS Express,Windows 8 Pro Web.config system.webServer system.webServer validation validateIntegratedModeConfiguration="false" / handlers remov
我为ASP.NET MVC4站点编写了一个生成图像的httpHandler.不调用ProcessRequest函数.有什么想法吗?

MVC4,IIS Express,Windows 8 Pro

Web.config> system.webServer

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <remove name="TextImage" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%Microsoft.NETFrameworkv4.0.30319aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%Microsoft.NETFramework64v4.0.30319aspnet_isapi.dll" preCondition="classicMode,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
      <add name="TextImage" path="textimage/*.png" verb="*" resourceType="Unspecified" type="MultiStepUI.TextImageHandler,MultiStepUI_MOBETTER" />
    </handlers>
  </system.webServer>

用法

<img src="/textimage/step1.png?q=Step 1&c=404040&w=30&h=250&z=12" />

解决方法

如果有人知道要查找什么,可以在网上找到答案.

MVC路由引擎尝试将所有请求映射到控制器 – 在这种情况下,这不是我们想要的.除了在Web.config中注册处理程序之外,我们还需要告诉MVC路由引擎忽略httpHandler路径,以便ASP.NET引擎可以处理它的路由.

我选择使用example from Phil Haack

为了对抗链接腐烂,这是文章的摘录

By default,ASP.NET Routing ignores requests for files that do not
exist on disk. I explained the reason for this in a previous post on
upcoming routing changes. Long story short,we didn’t want routing to
attempt to route requests for static files such as images.
Unfortunately,this caused us a headache when we remembered that many
features of ASP.NET make requests for .axd files which do not exist on
disk.

To fix this,we included a new extension method on
RouteCollection,IgnoreRoute,that creates a Route mapped to the
StopRoutingHandler route handler (class that implements
IRouteHandler). Effectively,any request that matches an “ignore
route” will be ignored by routing and normal ASP.NET handling will
occur based on existing http handler mappings. Hence in our default
template,you’ll notice we have the following route defined.

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

This handles the
standard .axd requests. However,there are other cases where you might
have requests for files that don’t exist on disk. For example,if you
register an HTTP Handler directly to a type that implements
IHttpHandler. Not to mention requests for favicon.ico that the browser
makes automatically. ASP.NET Routing attempts to route these requests
to a controller.
One solution to this is to add an appropriate ignore
route to indicate that routing should ignore these requests.
Unfortunately,we can’t do something like this:

{*path}.aspx/{*pathinfo}

We only allow one catch-all route and it must
happen at the end of the URL
. However,you can take the following
approach. In this example,I added the following two routes.

routes.IgnoreRoute(“{*allaspx}”,new {allaspx=@”.*.aspx(/.*)?”});
routes.IgnoreRoute(“{*favicon}”,new {favicon=@”(.*/)?favicon.ico(/.*)?”});

What I’m doing here is a technique Eilon showed me which is to map all URLs to these routes,but then restrict which routes to ignore via the constraints dictionary. So in this case,these routes will match (and thus ignore) all requests for favicon.ico (no matter which directory) as well as requests for a .aspx file. Since we told routing to ignore these requests,normal ASP.NET processing of these requests will occur.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读