我有一个在所有应用程序中使用的值; 我在application_start中设置了它
void Application_Start(object sender, EventArgs e)
{
Dictionary<int, IList<string>> Panels = new Dictionary<int, IList<string>>();
List<clsPanelSetting> setting = clsPanelSettingFactory.GetAll();
foreach (clsPanelSetting panel in setting)
{
Panels.Add(panel.AdminId, new List<string>() { panel.Phone,panel.UserName,panel.Password});
}
Application["Setting"] = Panels;
SmsSchedule we = new SmsSchedule();
we.Run();
}
Run Code Online (Sandbox Code Playgroud)
并在SmsSchedule
public class SmsSchedule : ISchedule
{
public void Run()
{
DateTimeOffset startTime = DateBuilder.FutureDate(2, IntervalUnit.Second);
IJobDetail job = JobBuilder.Create<SmsJob>()
.WithIdentity("job1")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1")
.StartAt(startTime)
.WithSimpleSchedule(x => x.WithIntervalInSeconds(60).RepeatForever())
.Build();
ISchedulerFactory sf = new StdSchedulerFactory();
IScheduler sc …Run Code Online (Sandbox Code Playgroud) 任何人都可以告诉我,是否有办法让我在global.asax的Application_Start事件中获取我的网站的域名?
通常我只是从Context.Request.ServerVariables ["SERVER_NAME"]中获取它,但这不可用.理想情况下,我还希望从启动应用程序的请求中获取URL.
嗯 - 从下面的答案来看,似乎在IIS7上有所作为.这是新的,现在有设计指南试图阻止你这样做:
在我的Global.asax.cs的Application_Start中,我试图使用以下方法获取当前的应用程序路径:
var virtualPath = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority)
+ HttpRuntime.AppDomainAppVirtualPath;
Run Code Online (Sandbox Code Playgroud)
这将返回例如: http://localhost:99/MySite/
然后我将使用此URL并执行以下操作:
var pageToHit = virtualPath + Pages\MyOtherPage.aspx
var client = new WebClient();
client.DownloadData(dummyPageUrl);
Run Code Online (Sandbox Code Playgroud)
当我在IIS 6或Visual Studio内置Web服务器中运行项目时,所有这一切都很好,但是当我得到"System.Web.HttpException:请求在此上下文中不可用"时,IIS 7中的事情变得疯狂.
我知道这个帖子:请求在此上下文中不可用
但是,我想知道是否有人知道如何在不改变项目的情况下执行上述操作以在经典模式下运行.
需求:
每天在特定时间(例如早上6点)动态地向所有用户发送电子邮件.
到目前为止我做了什么:
我使用Nuget的第三方库Quartz.net.
public class TaskScheduler : IJob
{
public void Execute(IJobExecutionContext context)
{
try {
UserRepository userRepo = new UserRepository();
var users = userRepo.GetUsers();
DashBoardRepository repo = new DashBoardRepository();
foreach (var rec in users)
{
var tasks = repo.GetIncompleteTasks(rec.UserID);
var appointments = repo.GetUpcomingAppointments(rec.UserID);
if (tasks.Count > 0 || appointments.Count > 0)
{
dynamic email = new Email("TaskEmail");
email.To = rec.Email;
email.From = "no-reply@xyz.com";
email.Subject = "Pending items in XYZ";
email.Tasks = tasks;
email.Appointments = appointments;
email.Send();
}
} …Run Code Online (Sandbox Code Playgroud) 在集成模式下使用HttpContext.Current.Request.ServerVariables ["SERVER_NAME"]会在IIS7中出现错误,如下所示:http://mvolo.com/blogs/serverside/archive/2007/11/10/Integrated-mode-Request-是-不提供功能于这个上下文功能于Application_5F00_Start.aspx
我可以在global.asax代码中使用HttpContext.Current.Request.ServerVariables ["SERVER_NAME"]吗?
这与使用类似
String strPath =
HttpContext.Current.Server.MapPath(HttpRuntime.AppDomainAppVirtualPath);
Run Code Online (Sandbox Code Playgroud)
代替
//String strPath =
HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ServerVariables["PATH_INFO"]);
Run Code Online (Sandbox Code Playgroud) 我正在创建一个http模块,我想检查一个请求是否来自经过身份验证的用户,如果不是,则重定向到登录页面.
我在web.config文件中注册了该模块,并且我有以下代码抛出异常:
public class IsAuthModule : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication TheApp)
{
var TheRequest = TheApp.Request;
}
}
Run Code Online (Sandbox Code Playgroud)
它抛出一个异常,说"在这种情况下请求不可用"
我究竟做错了什么?
我想知道如何检查我是使用我的本地主机还是在实时服务器上.
基本上我想...
if (using localhost)
{
Do Stuff...
}
else
{
Do this instead...
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?我四处寻找,但找不到任何东西.我想这样做,因为我对live和dev服务器有不同的设置.我想要一种自动检查以查看我正在使用的方法,然后使用某些设置.
提前致谢.
asp.net ×6
asp.net-mvc ×3
c# ×3
iis-7 ×3
iis ×2
.net ×1
httpcontext ×1
postal ×1
quartz.net ×1