我在asp.net中有Httphandler返回一个文件。在代码中我有Response.AddHeader("Content-Disposition", "attachment; filename=somefile.ext");
所以页面URL保持不变。
但是如果Httphandler中出现错误,URL会变成像http://localhost:55161/document.axd
这样的东西而且我有白屏。
如何在没有页面刷新的情况下返回来自Httphandler的javascript警报?像这样:
public class Document: IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;
//Response.AddHeader("Content-Disposition", "attachment; filename=somefile.ext");
Response.ContentType = "application/javascript";
Response.Write("<script type ='text/javascript'>alert('Error!');</script>");
}
public bool IsReusable
{
get { return false; }
}
}
对不起我的英语,我不是母语:)
答案 0 :(得分:1)
我认为你做错了。你不应该从http处理程序返回javascript。它们旨在用于处理数据。应根据Httphandler执行的结果显示警报。
你可以做的是在你的http处理程序中放置try catch块,如果有异常则返回特定的代码。然后在你的javascript中 - 检查该代码 - 并显示警告。
向下你可以找到我在这种情况下使用过的东西。它会给你一个想法 - 你有一个单独的类,它包含执行的结果 - 并返回易于通过javascript处理的JSON。
public void ProcessRequest(HttpContext context)
{
var result = new HandlerResult();
var resultJson = string.Empty;
var reqDictionary = Helper.DecryptQueryParams(context);
try
{
if (!Helper.AllQueryParametersExists(reqDictionary, "userid", "topN", "time", "latitude", "logitude", "shareLocation"))
{
_log.Error("Incomplete query string parameters!");
throw new Exception("Incomplete query string parameters!");
}
Guid userId;
Guid.TryParse(reqDictionary["userid"], out userId);
int topN;
int.TryParse(reqDictionary["topn"], out topN);
double minutes;
double.TryParse(reqDictionary["time"], out minutes);
var time = DateTime.Now.AddDays(-minutes);
double latitude;
double.TryParse(reqDictionary["latitude"], out latitude);
double logitude;
double.TryParse(reqDictionary["logitude"], out logitude);
bool shareLocation = Helper.ParseInt(reqDictionary["sharelocation"]);
var finalTime = new DateTime(time.Year, time.Month, time.Day, time.Hour, time.Minute, 0);
var users = _userAccessor.Repo.FindUsers(userId, topN, finalTime, latitude,
logitude, shareLocation);
var sb = new StringBuilder();
var list = new List<UserLocation>();
foreach (var user in users)
{
var userLocation = new UserLocation
{
UserId = user.Id,
UserName = user.Email,
FullName = user.FullName,
Gender = user.Gender,
Age = user.Age,
Latitude = user.Latitude,
Longitude = user.Longitude,
Time = user.Time
};
list.Add(userLocation);
}
context.Response.Write(sb.ToString());
result.Result = string.Empty;
result.ResultStatus = HandlerResult.Status.Successful;
resultJson = JsonConvert.SerializeObject(list);
_log.Info("OK");
}
catch (Exception ex)
{
result.ResultStatus = HandlerResult.Status.Error;
result.Result = ex.Message;
resultJson = JsonConvert.SerializeObject(result);
_log.Error(ex);
}
finally
{
context.Response.Clear();
context.Response.ContentType = "text/plain";
context.Response.Write(resultJson);
context.Response.Flush();
context.Response.End();
_log.Info(resultJson);
}
}
public class HandlerResult
{
public string Result { get; set; }
public Guid UserId { get; set; }
public Status ResultStatus { get; set; }
public HandlerResult(Status resultStatus, string result)
{
this.ResultStatus = resultStatus;
this.Result = result;
}
public HandlerResult(string result)
{
ResultStatus = Status.Novalue;
this.Result = result;
}
public HandlerResult()
{
}
public enum Status
{
Novalue = 0,
Successful = 1,
Error = 2,
Notvaliduser = 3,
Successfulupdate = 4,
UserExists = 5,
UserNotConfirmed = 6
}
}