2015-11-29 140 views
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没有被渲染。

回答

0

我只好一个Web.config添加到文件夹包含我的JSON

<?xml version="1.0"?> 
<configuration> 
    <system.webServer> 
     <staticContent> 
     <mimeMap fileExtension=".json" mimeType="application/json" /> 
     </staticContent> 
    </system.webServer> 
</configuration> 

我想你可以把它添加到你的根配置以及

+0

获得以下错误:无法添加重复的集合项键入具有唯一键属性'fileExtension'的'mimeMap'设置为'.json' – userda

+0

检查web.config中的部分。虽然,我怀疑它是否已经存在,但你的问题与我的问题不一样。 –

相关问题