我尝试添加自定义处理程序 - 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。
答案 0 :(得分:0)
我必须将Web.config添加到包含我的JSON
的文件夹中<?xml version="1.0"?>
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
</system.webServer>
</configuration>
我假设您也可以将其添加到根配置