我已经下载了MVC 3的源代码,以了解它是如何运行的.
许多人说MVC拦截了UrlRouting Moudle Class的Http请求.
我知道当你定制一个HttpModule时,你需要像这样注册它:
<system.webServer>
<modules>
<add name="test" type="WebApplication2.MyModule1,WebApplication2"/>
</modules>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
所以Asp.net mvc应用程序的Web.config文件应该有配置部分:
<add name="UrlRoutingModule"
type="System.Web.Routing.UrlRoutingModule,..." />
Run Code Online (Sandbox Code Playgroud)
但是当我创建一个新的Asp.net MVC应用程序时,我无法在web.config文件中找到它.
有人说IIS 7会自动添加它.
什么时候到IIS7添加配置部分?
IIS7如何区别它是MVC应用程序还是WebForm?
我有一个用asp.net编写的应用程序,我在网站中集成了一些传统的经典asp页面.该站点使用Windows身份验证.因为我无法管理带有角色的.asp页面,所以我编写了一个自定义HttpModule来检查用户是否有权查看这些页面,否则会重定向到"拒绝访问"页面.主要问题是应用程序需要在IIS7上以"经典模式"运行.我的模块在集成模式下工作,但在经典模式下不工作.有没有理由这个代码也不能在经典模式下工作?提前致谢.
这是模块的代码,它非常简单:
public class MyModule: IHttpModule
{
public void Init(HttpApplication application)
{
application.PostAuthenticateRequest += new EventHandler(Application_PostAuthenticateRequest);
}
void Application_PostAuthenticateRequest(object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
HttpContext context = ((HttpApplication)source).Context;
if (context.Request.RawUrl.Contains("/protected-subfolder/"))
{
// gets user from windows authentication
string currentUser = Convert.ToString(context.User.Identity.Name);
if (!isAdmin(currentUser))
{
//deny access
(context.Response).Redirect(VirtualPathUtility.ToAbsolute("~/AccessDenied.aspx"));
}
}
}
public void Dispose(){ }
Run Code Online (Sandbox Code Playgroud)
这是经典模式的web.config中的设置(不工作):
<configuration>
<system.web>
<httpModules>
<add name="MyModule" type="MyModule" />
</httpModules>
</system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)
并且集成模式(工作)的设置:
<configuration>
<system.webServer>
<modules>
<add name="MyModule" type="MyModule"/>
</modules>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer> …Run Code Online (Sandbox Code Playgroud) (我不知道是否也应该将此问题发布到ServerFault,因为它是关于IIS配置的?)
在IIS7中,我们可以告诉模块运行托管内容(从而加快静态内容服务):
<modules>
...
<add name="WhateverName"
type="WhateverType"
preCondition="managedHandler"
...
</modules>
Run Code Online (Sandbox Code Playgroud)
但.只要在请求的URL中还有文件名(带扩展名),这样就可以正常工作.如果省略它,IIS7会认为你想要静态内容,托管模块将无法运行.
http://localhost/ <-- this one will skip managed handlers
http://localhost/default.aspx <-- this one will run them
Run Code Online (Sandbox Code Playgroud)
如果我手动设置IIS7默认文件,那么第一个是default.aspx,我可以看到没有区别,没有区别.对我来说,这看起来像是一个臭虫,走路和听起来像是 这是一个错误!为什么?因为当我请求第一个时,它是一个托管请求,不是吗.当然如此.但IIS7将其视为静态请求.所以?这是一个错误.此请求应视为托管.
如何说服IIS7为没有文件名的URL请求运行托管处理程序?
让我帮你思考一下:如果我重新订购system.webServer/handlers,我肯定可以解决这个问题.最后才StaticFile指向处理器StaticFileModule,DefaultDocumentModule并且DirectoryBrowsingModule我要上运行的目录请求集成asp.net处理程序.或者编写我自己的处理程序,将默认文档附加到任何目录请求.我很确定其中一个应该解决它.但是我如何配置/开发它呢?
我有一个HttpModule,我把它从一些不同的在线源码拼凑在一起,形成了(大部分)与传统的ASP.NET应用程序以及ASP.NET MVC应用程序一起工作的东西.其中最大的部分来自CodePlex上的kigg项目.我的问题是处理由于图像丢失导致的404错误.在下面的代码中,我必须通过HttpContext的Request对象中的AcceptedTypes集合显式查找请求的图像.如果我没有进行此检查,即使丢失的图像也会导致重定向到Web.config中我的部分中定义的404页面.
这种方法的问题是(除了它闻到的事实)是这只是图像.我基本上必须对可以想象的每种内容类型执行此操作,我不希望发生此重定向行为.
看看下面的代码,是否有人可以推荐某种重构,这可能会使非页面请求变得更宽松?我仍然希望它们在IIS日志中(所以我可能不得不删除ClearError()调用),但我不认为破坏的图像会影响用户体验,直到将它们重定向到错误页面.
代码如下:
/// <summary>
/// Provides a standardized mechanism for handling exceptions within a web application.
/// </summary>
public class ErrorHandlerModule : IHttpModule
{
#region Public Methods
/// <summary>
/// Disposes of the resources (other than memory) used by the module that implements
/// <see cref="T:System.Web.IHttpModule"/>.
/// </summary>
public void Dispose()
{
}
/// <summary>
/// Initializes a module and prepares it to handle requests.
/// </summary>
/// <param name="context">
/// An <see cref="T:System.Web.HttpApplication"/> that provides access …Run Code Online (Sandbox Code Playgroud) asp.net error-handling asp.net-mvc httpmodule http-status-code-404
由于ASP.NET响应过滤和后缓存替换不兼容,我需要一种过滤输出的替代方法.参考:http://support.microsoft.com/kb/2014472
有没有办法在不使用响应过滤器的情况下更改标记?
我只是想澄清一下我对ASP .NET MVC的理解(当前版本是4).
我正在阅读这篇关于ASP.NET MVC如何工作的文章?
那么,ASP.NET如何知道如何将请求路由到MVC?答案在于web.config.ASP.NET MVC项目中的模块集合中添加了一个新的http模块
所以基本上mvc应用程序实现为HTTPModule或至少是mvc应用程序的url路由部分?
是否有可能创建和注册自定义路由模块,然后可能创建自己的微型mvc框架,如Ruby中的Sinatra或PHP中的Slim?
我使用MVC文件夹结构,其中URL路由恰好与目录名称匹配,例如:
<proj>\My\Cool\Thing\ThingController.cs
Run Code Online (Sandbox Code Playgroud)
需要通过以下网址访问:
http://blahblah/My/Cool/Thing
Run Code Online (Sandbox Code Playgroud)
我可以使用MVC路由,但是不幸的是,当依靠默认的{action}和{id}时,IIS Express会将请求路由到DirectoryListingModule,因为它直接与文件夹名称匹配。目录列表当然是禁用的,所以我得到了:
The Web server is configured to not list the contents of this directory.
Module DirectoryListingModule
Notification ExecuteRequestHandler
Handler StaticFile
Run Code Online (Sandbox Code Playgroud)
为了解决这个问题,我已经尝试过:
1. runAllManagedModulesForAllRequests = true
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" >
//Makes no difference
2. Removing module
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" >
<remove name="DirectoryListingModule"/>
// Won't let me as module is locked in IIS
</modules>
</system.webServer>
3. Removing lock & module
// applicationhost.config
<add name="DirectoryListingModule" lockItem="false" />
// web.config
<remove name="DirectoryListingModule"/>
// Causes startup error"Handler "StaticFile" has a …Run Code Online (Sandbox Code Playgroud) 我有一个HttpModule应该限制访问/courses/我的网站目录的实现,但它有一个主要问题.
Request.IsAuthenticated永远false.
这是代码:
using System;
using System.Web;
public class CourseAuthenticationModule : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(BeginRequest);
}
public void BeginRequest(Object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
HttpContext context = app.Context;
HttpRequest request = context.Request;
HttpResponse response = context.Response;
if (request.Path.ToLower().StartsWith("/courses/")
&& !request.IsAuthenticated)
{
response.Redirect("/");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么会发生这种情况,但条件总是会true在访问/courses/目录时进行评估.
编辑:
我在Web.Config中找到了这个.不确定它是否相关.
<authentication mode="Forms">
<forms loginUrl="userlogin.asp" …Run Code Online (Sandbox Code Playgroud) 在ASP.NET Web API中,HTTP请求和响应在服务器上的管道中处理.
如果我想在管道的最初阶段添加一些逻辑或全局行为,我应该在消息处理程序中执行它.(例如:身份验证)
但是Http模块管道怎么样?它适合这整个故事?
查看web api生命周期的这个编号阶段:
http://i.stack.imgur.com/jkQe8.jpg

但是看一下Http模块的一般事件(包含更多但是......)
题 :
- 这两个系统如何结合在一起?我的意思是如果有1张包含web api和http模块的图片,那么这些数字是多少?(我在图像中添加了数字以便于参考)
- 我总是听说如果我想在管道中更早地做事情,我应该使用消息处理程序,但是BeginRequest例如HttpModule 呢?我知道在这个阶段有些对象是空的但是,httpsodule的后期阶段确实会对HttpContetxt的对象进行充气 - 然而,webapi的人说:使用MessageHandlers ....(它是否与selfhoster环境有关)?
我正在创建一个HTTPModule,可以重复使用几次,但参数不同.以一个请求重定向器模块为例.我可以使用HTTPHandler,但它不是一个任务,因为我的进程需要在请求级别工作,而不是在扩展/路径级别.
无论如何,我想以这种方式拥有我的web.config:
<system.webServer>
<modules>
<add name="tpl01" type="TemplateModule" arg1="~/" arg2="500" />
<add name="tpl02" type="TemplateModule" arg1="~/" arg2="100" />
</modules>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
但我能找到的大部分信息都是这样的.我说,是的,我可以获得整个<modules>标记,但我的HTTPModule的每个实例如何知道要采用哪些参数?如果我可以在创建时获得名称(tpl01或tpl02),我可以在之后通过名称查看其参数,但我没有在HTTPModule类中看到任何属性来获取它.
任何帮助都会非常受欢迎.提前致谢!:)
httpmodule ×10
asp.net ×6
asp.net-mvc ×4
iis-7 ×3
c# ×2
web-config ×2
.net ×1
arguments ×1
httpcontext ×1
httphandler ×1
httprequest ×1
iis ×1