【发布时间】:2016-03-03 07:52:24
【问题描述】:
我正在尝试添加自定义处理程序 - DayOfWeekHandler(参考Pro ASP.NET MVC 5 Platform Book 进行设置)。
routes.config 中的路由:
routes.Add(new Route("handler/path", new CustomHandler()
{ HandlerType = typeof(DayOfWeekHandler) }));
自定义处理程序:
public class CustomHandler : IRouteHandler
{
public Type HandlerType;
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return (IHttpHandler)Activator.CreateInstance(HandlerType);
}
}
当我在浏览器中输入“http://localhost:81/handler/path”时 - 它正确地从处理程序调用 ProcessRequest 方法,但是当我输入“http://localhost:81/handler/path.json”时,我收到 404.0 错误:
HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable. In this case it is not even calling Process Request method.
DayOfWeekHandler 中的ProcessRequest 方法:
public void ProcessRequest(HttpContext context)
{
string day = DateTime.UtcNow.DayOfWeek.ToString();
context.Response.Write($"Hello from {GetType().Name} Handler :)");
if (context.Request.CurrentExecutionFilePathExtension == ".json")
{
context.Response.ContentType = "application/json";
context.Response.Write($" \"day\" : \"{day}\" ");
}
else
{
context.Response.ContentType = "text/html";
context.Response.Write($"<div>It is {day}!</div><br/>");
}
}
此外,当处理程序在 web.config 文件中注册时,它工作正常。 我是不是错过了什么。请帮助我理解为什么 /handler/path.json 没有被渲染。
【问题讨论】:
标签: c# asp.net-mvc routes httphandler global-asax