简体   繁体   English

如何在URL加载后立即运行aspx.cs文件

[英]How to run aspx.cs file immediately on url load

sorry I am not exactly sure how to ask this question. 对不起,我不确定如何问这个问题。

What I am trying to do is create a web service that when the url is hit (a .swf file will send the request and send information) the web service send an email. 我要尝试做的是创建一个Web服务,当URL被命中时(.swf文件将发送请求并发送信息),该Web服务将发送电子邮件。

So basically if a browser hits a url, say mywebsite.com/smtp/ it loads a aspx.cs file that sends an email. 因此,基本上,如果浏览器访问了URL,则说mywebsite.com/smtp/它将加载一个发送电子邮件的aspx.cs文件。 I do not want to have to have the browser open mywebsite.com/smtp/sendemail.aspx. 我不需要浏览器打开mywebsite.com/smtp/sendemail.aspx。

Basically I just want to be able to run a .cs file on my webserver by just hitting the link. 基本上,我只想通过单击链接就可以在Web服务器上运行.cs文件。

I assume I just need a bit of code in the .aspx file to immediately load the aspx.cs file. 我假设我只需要.aspx文件中的一些代码即可立即加载aspx.cs文件。

Any thoughts or help? 有什么想法或帮助吗? Sorry I did research I am just not sure how to phrase the question in google. 抱歉,我做过研究,只是不确定如何在Google中表达问题。

There are many ways of doing that. 有很多方法可以做到这一点。 The easiest I think would be an HTTPHandler. 我认为最简单的是HTTPHandler。

http://support.microsoft.com/kb/308001 http://support.microsoft.com/kb/308001

First I suggest you to use one handler for that (ends with .ashx) 首先,我建议您为此使用一个处理程序(以.ashx结尾)

Now, there is not such a think like the cs runs first, the aspx second. 现在,没有像cs首先运行,aspx其次运行这样的想法。 There is a full page that runs, do some stuff (one can be the send of an email), renders and return an html page. 有一个完整的页面可以运行,可以执行一些操作(可以发送一封电子邮件),呈现并返回一个html页面。

In that part you can simple cancel the return of the html by sending the no content as: 在那部分中,您可以通过发送no内容来简单地取消html的返回:

Response.TrySkipIisCustomErrors = true;
Response.Status = "204 No Content";
Response.StatusCode = 204;

and end the page cycle with the Response.End(); 并使用Response.End();结束页面循环Response.End(); , or use a handler as I say you that you do not need that. ,或者使用处理程序,就像我说的那样,您不需要。

If I understand the question, you can do this just by setting a default page on that directory (in the IIS settings). 如果我理解这个问题,则可以通过在该目录上(在IIS设置中)设置默认页面来执行此操作。 So you would set sendemail.aspx as the default page in the smtp virtual directory, and requests for /smtp/ would be handled by sendemail.aspx. 因此,您可以将sendemail.aspx设置为smtp虚拟目录中的默认页面,对/ smtp /的请求将由sendemail.aspx处理。

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

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