本文介绍了使用自定义MvcHttpHandler V2.0最新变化,从1.0到2.0吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,其中部分是web表单(一把umbraco CMS)和部分是MVC
这是对付MVC功能的HttpHandler:

I have a site where part is webforms (Umbraco CMS) and part is MVCThis is the HttpHandler to deal with the MVC functionality:

public class Mvc : MvcHttpHandler
{
    protected override void ProcessRequest(HttpContext httpContext)
    {
        httpContext.Trace.Write("Mvc.ashx", "Begin ProcessRequest");
        string originalPath = httpContext.Request.Path;
        string newPath = httpContext.Request.QueryString["mvcRoute"];
        if (string.IsNullOrEmpty(newPath))
            newPath = "/";

        httpContext.Trace.Write("Mvc.ashx", "newPath = "+newPath );

        HttpContext.Current.RewritePath(newPath, false);
        base.ProcessRequest(HttpContext.Current);
        HttpContext.Current.RewritePath(originalPath, false);
        }
}

这是如何在这里实现 ;一切编译,但在运行时我得到这个异​​常:

Full details of how this is implemented here This method works well in an MVC 1.0 website.However when I upgrade this site to MVC 2.0 following the steps in Microsoft's upgrade documentation; everything compiles, except at runtime I get this exception:

服务器中的/应用程序错误。结果
  资源无法找到。结果
  说明:HTTP 404资源
  你正在寻找(或它的一个
  依赖关系)可能已被移除,
  更名,或
  暂时不可用。请
  检查以下URL并确保
  它拼写正确。

请求的URL:/mvc.ashx

Requested URL: /mvc.ashx

版本信息:微软.NET
  Framework版本:2.0.50727.4927;
  ASP.NET版本:2.0.50727.4927

Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927

这个资源,它的依赖被发现罚款MVC 1.0,但不是在MVC 2.0,有一个额外的依赖我需要补充的吗?
有我丢失的东西?是否有方式的改变MVC 2.0工程?

This resource and it's dependencies are found fine in MVC 1.0 but not in MVC 2.0, is there an extra dependency I'd need to add?Is there something I'm missing? Is there a change in the way MVC 2.0 works?

推荐答案

一个忠告 - 在MvcHandler和MvcHttpHandler类型并不意味着用户code被继承。这些处理器将与框架的未来版本的变化,所以它继承他们的任何类型都受到破坏。考虑到这一点。

A word of caution - the MvcHandler and MvcHttpHandler types are not meant to be subclassed by user code. These handlers will change with future versions of the framework, so any types which subclass them are subject to breaking. With that in mind..

在MVC 2中,MvcHttpHandler类型是的 IHttpAsyncHandler 的,不只是一个的的IHttpHandler 的。这改变了ASP.NET如何执行处理程序的语义。如果子类MvcHttpHandler,你需要重写的 BeginProcessRequest 的和的 EndProcessRequest 的除了方法的的ProcessRequest 的方法。

In MVC 2, the MvcHttpHandler type is an IHttpAsyncHandler, not just an IHttpHandler. This changes the semantic of how ASP.NET executes the handler. If you subclass the MvcHttpHandler, you need to override the BeginProcessRequest and EndProcessRequest methods in addition to the ProcessRequest method.

一个更安全的机制是包住MvcHttpHandler,而不是继承它。也就是说,使自己的IHttpHandler的ProcessRequest的()方法只委托给的新MvcHttpHandler.ProcessRequest()的。通过这种方式,改变MvcHttpHandler的工作应该不会造成您的包裹处理打破。

A safer mechanism would be to wrap the MvcHttpHandler instead of subclassing it. That is, make your own IHttpHandler whose ProcessRequest() method just delegates to new MvcHttpHandler.ProcessRequest(). This way, changes to the working of MvcHttpHandler shouldn't cause your wrapping handler to break.

这篇关于使用自定义MvcHttpHandler V2.0最新变化,从1.0到2.0吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 20:39