简体   繁体   English

IIS7 文件映射 -.asax、.ashx、.asap

[英]IIS7 file mappings - .asax, .ashx, .asap



IIS enables us to also configure Asp.Net file mappings. IIS 使我们还可以配置 Asp.Net 文件映射。 Thus besides aspx, IIS also invokes Asp.Net runtime, when requests have the following file extensions:因此,除了 aspx,IIS 还调用 Asp.Net 运行时,当请求具有以下文件扩展名时:

a).ascx -->.asmx extension is used to request user controls. a).ascx -->.asmx 扩展名用于请求用户控件。

  • Since user controls can't be accessed directly, how and why would anyone send a request to a user control?由于无法直接访问用户控件,那么任何人如何以及为什么会向用户控件发送请求?

b).ashx --> this extension is used for HTTP handlers. b).ashx --> 此扩展名用于 HTTP 处理程序。

• But why would you want to request an.ashx page directly instead of registering this handler inside configuration file and enable it to be called when files with certain ( non ashx ) extensions are requested? • 但是为什么要直接请求一个.ashx 页面,而不是在配置文件中注册这个处理程序并在请求具有某些(非ashx)扩展名的文件时调用它呢?

• Besides, since there can be several Http handlers registered, how will Asp.Net know which handler to invoke if they all use ashx extension? • 此外,由于可以注册多个 Http 处理程序,如果它们都使用 ashx 扩展,Asp.Net 将如何知道调用哪个处理程序?

• What does the requested ashx file contain? • 请求的ashx 文件包含什么? Perhaps a definition of a Http handler class?也许是 Http 处理程序 class 的定义?

• I know how we register Http handlers to be invoked when non-ashx pages are requested, but how do we register Http handler for ashx page? • 我知道我们如何注册 Http 处理程序以在请求非 ashx 页面时调用,但是我们如何为 ashx 页面注册 Http 处理程序?



c).asax --> This extension is used to request a global application file c).asax --> 此扩展名用于请求全局应用程序文件

• Why would we ever want to call Global.asax directly? • 我们为什么要直接调用 Global.asax?

• I assume that when request is made for Global.asax, an object derived from HTtpApplication class is created, except this time no web page processing takes place? • 我假设当请求 Global.asax 时,会创建一个从 HTtpApplication class 派生的 object,除了这次没有 web 页面处理发生?



thanx谢谢




Q - Besides Asp.Net being able to request global.asax for compilation, is there any other reason why I would choose to request file with.asax extension directly?问 - 除了 Asp.Net 能够请求 global.asax 进行编译之外,还有什么其他原因可以选择直接请求扩展名为 .asax 的文件吗?


• ashx files don't have to be registered. • ashx 文件不必注册。 They are basically a simpler aspx, for when you don't need the entire page life cycle.它们基本上是一个更简单的 aspx,当您不需要整个页面生命周期时。 A common use is for retrieving dynamic images from a database.一个常见的用途是从数据库中检索动态图像。

So if I write a Http handler, I should put it in a file with.ashx extension and Asp.Net will build an HttpHandler object similarly to how it builds a page instance from.aspx file?所以如果我写一个 Http 处理程序,我应该把它放在一个扩展名为 .ashx 的文件中,Asp.Net 将构建一个 HttpHandler object 类似于它如何构建一个页面实例 from.aspx 文件?


• If a hacker did try to make a request for one of these files, what would you want to happen? • 如果黑客确实尝试请求这些文件之一,您希望发生什么? You certainly wouldn't want IIS to treat it like a text file and send the source for your app down to the browser.您当然不希望 IIS 将其视为文本文件并将您的应用程序的源代码发送到浏览器。

Asp.Net could do the same it does with.cs, .csproj, .config, .resx, .licx, .webinfo file types. Asp.Net 可以对 .cs、.csproj、.config、.resx、.licx、.webinfo 文件类型做同样的事情。 Namely, it registers these file types with IIS so that it can explicitly prevent users from accessing these files即,它将这些文件类型注册到 IIS 以便它可以明确地阻止用户访问这些文件


•Just because you don't expect requests from the browser for a resource, it doesn't mean you don't want that resource handled by the asp.net engine. • 仅仅因为您不希望浏览器请求资源,并不意味着您不希望该资源由 asp.net 引擎处理。 These extensions are also how ASP.Net picks up files to compile for the web site model sites.这些扩展也是 ASP.Net 为 web 站点 model 站点选择要编译的文件的方式。

But then why doesn't Asp.Net also allow.cs, .csproj, .config, .resx, .licx, .webinfo files to be directly requested?但是为什么 Asp.Net 也不允许直接请求.cs、.csproj、.config、.resx、.licx、.webinfo 文件呢?



a) and c) - as far as I am aware, these are not exposed to process any external requests a) 和 c) - 据我所知,这些不用于处理任何外部请求

my book claims the two are mapped in IIS我的书声称这两个映射在 IIS



I appreciate your help我感谢您的帮助

EDIT:编辑:

b) The.ashx extention is defined in a config file it's just not the web.config, its in the machine.config b).ashx 扩展名是在配置文件中定义的,它不是 web.config,而是在 machine.config 中

<add path="*.ashx" verb="*" type="System.Web.UI.SimpleHandlerFactory" validate="True" />
http://msdn.microsoft.com/en-us/library/bya7fh0a.aspx http://msdn.microsoft.com/en-us/library/bya7fh0a.aspx

Why use.ashx: The difference is that the .NET class that handles a.ashx reads the Page directive in the.ashx file to map the request to a class specified in that directive. Why use.ashx: The difference is that the .NET class that handles a.ashx reads the Page directive in the.ashx file to map the request to a class specified in that directive. This saves you from having to put an explicit path in the web.config for every handler that you have, which could result in a very long web.config.这使您不必为您拥有的每个处理程序在 web.config 中放置一个显式路径,这可能导致非常长的 web.config。

I thought Http handler class was defined inside.ashx file, but instead file with.ashx extension only contains Page directive?我以为 Http 处理程序 class 是在 .ashx 文件中定义的,但是扩展名为 .ashx 的文件只包含 Page 指令?

Since I'm not 100% sure if I understand this correctly: Say we have ten Http handlers we want to invoke by making a request to IIS7.因为我不能 100% 确定我是否理解正确:假设我们有十个 Http 处理程序,我们想通过向 IIS7 发出请求来调用。 I assume for each Http handler there will be specific.ashx file --> thus if request is made for FirstHandler.asxh, then handler specified inside that file will be invoked?我假设每个 Http 处理程序都会有特定的.ashx 文件-> 因此,如果对 FirstHandler.asxh 发出请求,那么将调用该文件中指定的处理程序?

YET ANOTHER EDIT:另一个编辑:

I must confess that I'm still a bit unsure about ashx extension.我必须承认,我仍然对 ashx 扩展有点不确定。

I realize that by using it we can for example create 'hey.ashx' page, where Page directive will tell which class ( Http handler) to invoke when request is made for 'hey.ashx' – thus no need to register Http handler in web.config.我意识到通过使用它,我们可以例如创建“hey.ashx”页面,其中 Page 指令将告诉在请求“hey.ashx”时调用哪个 class(Http 处理程序)——因此无需在 5A568F05B3955 中注册 Z9D4D43DE568F05B3955 处理程序web.config。

But if you use Http handlers that way, then they will only get invoked when requests are made for files with.ashx extension.但是,如果您以这种方式使用 Http 处理程序,那么它们只会在请求扩展名为 .ashx 的文件时被调用。 Thus, if I want Http handler to be invoked for files with other extensions, such as.sourceC, then I will still need to register Http handler in web.config?!因此,如果我希望为具有其他扩展名的文件(例如 .sourceC)调用 Http 处理程序,那么我仍然需要在 web.config 中注册 Http 处理程序?!

A few points:几点:

  • asmx files are not the same as ascx files. asmx 文件与 ascx 文件不同。 You use them for web services (soap) rather than web controls.您将它们用于 web 服务(肥皂)而不是 web 控件。
  • ashx files don't have to be registered. ashx 文件不必注册。 They are basically a simpler aspx, for when you don't need the entire page life cycle.它们基本上是一个更简单的 aspx,当您不需要整个页面生命周期时。 A common use is for retrieving dynamic images from a database.一个常见的用途是从数据库中检索动态图像。
  • If a hacker did try to make a request for one of these files, what would you want to happen?如果黑客确实尝试请求这些文件之一,您希望发生什么? You certainly wouldn't want IIS to treat it like a text file and send the source for your app down to the browser.您当然不希望 IIS 将其视为文本文件并将您的应用程序的源代码发送到浏览器。
  • Just because you don't expect requests from the browser for a resource, it doesn't mean you don't want that resource handled by the asp.net engine.仅仅因为您不期望来自浏览器的资源请求,并不意味着您不希望 asp.net 引擎处理该资源。 These extensions are also how ASP.Net picks up files to compile for the web site model sites.这些扩展也是 ASP.Net 为 web 站点 model 站点选择要编译的文件的方式。

a) and c) - as far as I am aware, these are not exposed to process any external requests a) 和 c) - 据我所知,这些不用于处理任何外部请求

b) by default, it will look for a.ashx file with the path/name requested. b) 默认情况下,它将查找具有请求的路径/名称的 .ashx 文件。 This makes it really easy to add a handler to a web site, with no configuration necessary.这使得将处理程序添加到 web 站点变得非常容易,无需进行任何配置。

Update: In a you also mentioned asmx.更新:在你还提到 asmx. My take is the book is explaining some ajax related feature, with some comments regarding:我的看法是这本书解释了一些 ajax 相关功能,并附有一些评论:

  • Asp.net doesn't allow making requests pointed to.ascx. Asp.net 不允许发出指向.ascx 的请求。
  • You can make a request to a web service (.asmx) to get you the info.您可以向 web 服务 (.asmx) 发出请求以获取信息。
  • There are some built in features to help you with the above.有一些内置功能可以帮助您完成上述操作。

a) .ascx can't be accessed directly becasue the default handler is the class System.Web.HttpForbiddenHandler a) .ascx 不能直接访问,因为默认处理程序是 class System.Web.HttpForbiddenHandler

<add path="*.ascx" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />

.asmx files can be called directly, they are webmethods (though you usually have to make POST request, unless you specify to allow GET's in the web.config .asmx 文件可以直接调用,它们是 web 方法(尽管您通常必须发出 POST 请求,除非您在 web.config 中指定允许 GET

b) The.ashx extention is defined in a config file it's just not the web.config, its in the machine.config b) .ashx 扩展名在配置文件中定义的,它不是 web.config,而是在 machine.config 中

<add path="*.ashx" verb="*" type="System.Web.UI.SimpleHandlerFactory" validate="True" />

http://msdn.microsoft.com/en-us/library/bya7fh0a.aspx http://msdn.microsoft.com/en-us/library/bya7fh0a.aspx

Why use.ashx: The difference is that the .NET class that handles a.ashx reads the Page directive in the.ashx file to map the request to a class specified in that directive. Why use.ashx: The difference is that the .NET class that handles a.ashx reads the Page directive in the.ashx file to map the request to a class specified in that directive. This saves you from having to put an explicit path in the web.config for every handler that you have, which could result in a very long web.config.这使您不必为您拥有的每个处理程序在 web.config 中放置一个显式路径,这可能导致非常长的 web.config。

-- --

c) Global.asax: i don't use gloabl.asax, i rather use the very elegant HttpModule solution, but it's probably setup for legacy sites that had global.asax files. c) Global.asax:我不使用 gloabl.asax,我宁愿使用非常优雅的 HttpModule 解决方案,但它可能是为具有 global.asax 文件的旧站点设置的。

To definitely clear any confusion you might have on what asp.net does with these requests, check the web.config in:要明确清除您可能对 asp.net 对这些请求所做的任何混淆,请检查 web.config 中的:

%systemroot%\Microsoft.NET\Framework\v2.0.50727\CONFIG

As you can see (posted mine below), asp.net excludes pretty much any of the files that you are unsure if they were receiving special treatment.如您所见(在下面发布),asp.net 几乎排除了您不确定是否接受特殊处理的所有文件。 Notice there is *.cs, *.acsx, *.asax.注意有 *.cs、*.acsx、*.asax。

<add path="*.asax" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.ascx" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.master" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.skin" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.browser" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.sitemap" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.dll.config" verb="GET,HEAD" type="System.Web.StaticFileHandler" validate="True"/>
<add path="*.exe.config" verb="GET,HEAD" type="System.Web.StaticFileHandler" validate="True"/>
<add path="*.config" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.cs" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.csproj" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.vb" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.vbproj" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.webinfo" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.licx" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.resx" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.resources" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.mdb" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.vjsproj" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.java" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.jsl" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.ldb" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.ad" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.dd" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.ldd" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.sd" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.cd" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.adprototype" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.lddprototype" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.sdm" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.sdmDocument" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.mdf" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.ldf" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.exclude" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
<add path="*.refresh" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>

Also, bear in mind that IIS might not be configured to map some requests (MIME types) to the ASP.NET pipeline.此外,请记住 IIS 可能未配置为 map 对 ASP.NET 管道的某些请求(MIME 类型)。

So does anyone have examaple for mime.types file for.ashx extension, like for example I have for aspx ->那么有没有人有 mime.types 文件 for.ashx 扩展名的例子,例如我有 aspx ->

 application/aspx                                aspx

I tried this and it don't work:我试过这个,但它不起作用:

application/ashx                                ashx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM