1
我正在嘗試添加自定義處理程序 - DayOfWeekHandler(Pro ASP.NET MVC 5 Platform Book用於設置)。JSON擴展路徑未由自定義處理程序處理
路線在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.
ProcessRequest方法在DayOfWeekHandler:
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沒有被渲染。
獲得以下錯誤:無法添加重複的集合項鍵入具有唯一鍵屬性'fileExtension'的'mimeMap'設置爲'.json' – userda
檢查web.config中的部分。雖然,我懷疑它是否已經存在,但你的問題與我的問題不一樣。 –