我並沒有按網上的方法調試成功,所以靠自己解決它。
1. 從官方下載 MVC 4 源碼。
2. 按以下順序,新建項目,不要強名稱,如果報錯,手動解決,大部分是把 internal 關鍵字改為 public . 為了速度,可批量替換。
1.System.Web.Razor
2.System.Web.WebPages.Deployment
3.System.Web.WebPages
4.System.Web.Helpers
5.System.Web.WebPages.Razor
最后是
System.Web.Mvc
3. 打開項目 , 去除以上6個官方dll , 引用自己編譯的dll 。
4. 在web.config 和 MVC 的各個 web.config 中,把 以上六個 dll 的 PublicKeyToken 設置為 null , 可批量替換 , 並且根web.config 做如下修改
<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Web.Helpers" publicKeyToken="null" /> <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.1.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Mvc" publicKeyToken="null" /> <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.WebPages" publicKeyToken="null" /> <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.1.0.0" /> </dependentAssembly> </assemblyBinding> </runtime>
重新編譯,即可調試。
順便記錄一下,網上所說的 MVC3 解決 檢測到有潛在危險的Request.Form 值 , 只能解決單個Action . 而我想 全局設置不檢測客戶端 Post 的值。
使用以上方法, 設置出錯中斷。 在 DefaultModelBinder . ShouldPerformRequestValidation 方法有如下判斷:
private static bool ShouldPerformRequestValidation(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (controllerContext == null || controllerContext.Controller == null || bindingContext == null || bindingContext.ModelMetadata == null) { // To make unit testing easier, if the caller hasn't specified enough contextual information we just default // to always pulling the data from a collection that goes through request validation. return true; } // We should perform request validation only if both the controller and the model ask for it. This is the // default behavior for both. If either the controller (via [ValidateInput(false)]) or the model (via [AllowHtml]) // opts out, we don't validate. return (controllerContext.Controller.ValidateRequest && bindingContext.ModelMetadata.RequestValidationEnabled); }
所以只需在 Controller 的基類進行如下設置:
public class BaseController: Controller { public BaseController() { this.ValidateRequest = false ; } }
即可。