0
我使用的是asp.net 4.5,並具有以下控制器。無法獲取ASP.Net控制器來處理JSON發佈數據
public class AccountController : Controller
{
//
// POST: /Account/JsonLogin
[System.Web.Mvc.AllowAnonymous]
[System.Web.Mvc.HttpPost]
public JsonResult Login(LogIndto loginRequest)
{
if (loginRequest.Email == null)
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Unauthorized));
return null;
}
}
這裏是基於提琴手我的DTO
public class LogIndto
{
public String Email { get; set; }
public String Password { get; set; }
public Boolean RememberMe { get; set; }
}
這就是被貼到我的服務
POST host/Account/Login HTTP/1.1
Host: host
Connection: keep-alive
Content-Length: 71
Accept: application/json, text/javascript, */*; q=0.01
Origin: clientDifferentThanHost
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer:clientdifferentThanHost/?
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
{"Email":"[email protected]","Password":"afasdf","RememberMe":false}
我希望ASP.Net的JSON轉換爲C#對象而是loginRequest中的所有數據都爲null(除了默認值爲false的布爾值)
這裏是我正在使用的JavaScript
this.Login = function() {
var dto = JSON.stringify({Email:"[email protected]",Password:"avafa",RememberMe:false});
$.ajax({
url: app.homeUrl + "Account/Login",
data: dto,
success: self.loginSuccess,
processData: false,
type: "Post",
dataType: "json",
traditional: true
}).fail(self.loginFail)
;
};
當我嘗試設置contentType:「application/json; charset = utf-8「這個請求永遠不會對服務產生影響,我認爲這是我使用的Thinktecture.IdenityModel交叉源代碼包中的一個錯誤,但是一旦我能夠完成這個工作,它就會嘗試着解決這個問題。幫助將不勝感激。我已經使用招我已經能夠通過以下以及
得到這在過去工作,我敢肯定,我只是需要一個第二雙眼睛幫助。Content-Type: application/json; charset=UTF-8
Referer: http://localhost:8900/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
{"loginRequest":[{"Email":"[email protected]","Password":"avafa","RememberMe":false}]}
仍然沒有值被正確地處理
看起來它是很容易配置Thinktecture以接受不同的contentType。http://encosia.com/using-cors-to-access-asp-net-services-across-domains/ – BigUnit5150