用动态创建的url c#发送电子邮件
本文关键字:电子邮件 url 动态 创建 | 更新日期: 2023-09-27 18:18:51
我想完成的:
我有一个Windows服务,创建基于一些数据库数据的电子邮件。(没问题)
我希望服务能够创建一个一次性使用的链接,沿着http://thisserver:80/someGuid/whicheverURL
的行然后监听这个Url,直到有人按下链接(我猜它可能是某种rest 'post'的东西)
我需要什么:
- 使用SelfHosted WebApi为这个url创建一个运行时url和监听器的选项是什么 关于如何做到这一点的一些指导(不必是具体的)例子。
- 查看通过Get 访问了哪个"guid"
牢记
我不需要显示任何数据,只需检查用户是否实际单击了链接,然后运行一些内部进程
一如既往,提前感谢:)
解决方案我找到了一个合适的解决方案->基于标记的答案&基于http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api
我在WindowsService(没有IIS)中自托管"站点"
主:var baseAddress = new Uri("http://localhost:8080/");
var guidList = new string[]{
"89ac3d67-81fc-4294-bacc-72a97469cc95",
"99ac3d67-81fc-4294-bacc-72a97469cc95",
"09ac3d67-81fc-4294-bacc-72a97469cc95",
};
foreach (var guid in guidList)
{
Console.WriteLine(guid);
var config = new HttpSelfHostConfiguration(baseAddress + guid);
config.Routes.MapHttpRoute("default", "{controller}/{id}", new { controller = new JobController(), id = RouteParameter.Optional });
var server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();
}
Console.WriteLine("Server is opened");
Console.ReadKey();
控制器 public HttpResponseMessage Get()
{
var request = ControllerContext.RequestContext.Url.Request.RequestUri;
Uri UrlGuid = request.RequestUri;
Console.WriteLine("{0} has been accessed", UrlGuid.Segments[1]);
return new HttpResponseMessage
{
Content = new StringContent("Some Content Here")
};
}
使用Web API或MVC:
- 我将用
- Url可以简单地是:
http://servername:80/controller?id=<the guid>
然而,自定义路由,你可以用你最初想要的方式创建URI的GUID在URI - 让控制器接收guid key并将记录标记为已消耗。
UNIQUEIDENTIFIER
(c# GUID)在表中创建一行,用于标识记录。MVC 5.0中的控制器示例
public class TestController: Controller
{
public ActionResult ProcessResponse(Guid id)
{
//
//lookup & process the record using the id...
//
return View("ThankYou"); // <-- will display the "ThankYou.cshtml" view
}
}