У меня есть базовый класс контроллера, который пытается взглянуть на Request.ContentType, чтобы узнать, является ли это запросом json или обычным запросом HTML. Затем базовый контроллер устанавливает простое перечисление для базового класса, и соответствующий контроллер возвращает правильный тип. Однако Request.ContentType всегда является пустой строкой. почему это?
мой базовый контроллер:
namespace PAW.Controllers
{
public class BaseController : Controller
{
public ResponseFormat ResponseFormat { get; private set; }
public User CurrentUser { get; private set; }
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
Culture.SetCulture();
if (this.Request.ContentType.ToLower() == "application/json")
this.ResponseFormat = ResponseFormat.Json;
else
this.ResponseFormat = ResponseFormat.Html;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
//setup user object
this.CurrentUser = WebUser.CurrentUser;
ViewData["CurrentUser"] = WebUser.CurrentUser;
}
}
public enum ResponseFormat
{
Html,
Json,
Xml
}
}
мой JQuery:
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "/Vendor/Details/" + newid,
data: "{}",
dataType: "json",
success: function(data) { ShowVendor(data); },
error: function(req, status, err) { ShowError("Sorry, an error occured (Vendor callback failed). Please try agian later."); }
});