그냥 비슷한 충족하기 위해 내 컨트롤러 아래 웹 API 코드를 제공 해요
요구 사항. C# MVC Controller에서 외부 RESTful 웹 API로 객체를 게시하도록 지정되었습니다. 앞으로는 웹 API는 그대로 유지되지만 C# MVC는 NodeJS/Angular 응용 프로그램으로 대체 될 수 있습니다. 그래서 내가 한 것은 직렬화 된 JSON 형식의 TempData에 개체를 할당 한 다음 페이지가 리디렉션되는보기에서 조건부로 AngularJS를 추가하고 AngularJS 게시물을 외부 WebAPI에 구현하는 것입니다. 귀하의 경우, TempData이 같은 보일 것이다 :
this.TempData["lead"] = new JavaScriptSerializer().Serialize(this.Json(lead, JsonRequestBehavior.AllowGet).Data);
그런 다음, 재보기 "매개 변수"에서를,이 각도 코드를 추가 할 수 있습니다
@if (this.TempData["lead"] != null)
{
<script type="text/javascript" src="@Url.Content("~/Contents/Scripts/angular.js")"></script>
<script type="text/javascript">
angular
.module('app', [])
.controller('controllerName', ['$http', '$scope', 'apiFactory', function ($http, $scope, apiFactory) {
var leadRecord = '@Html.Raw(this.TempData["lead"])';
var apiUrl = 'https://xxxxxxxxxxxxxx';
apiFactory({
method: 'POST',
url: apiUrl + '/api/apiControllerName/Post',
data: '=' + leadRecord,
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' }
}).then(function (result) {
console.log(result);
});
}])
.factory('apiFactory', function ($http, $q) {
return function (config) {
var defered = $q.defer();
$http(config)
.success(function (result, status, headers, config) {
defered.resolve(result);
})
return defered.promise;
}
})
</script>
}
<div ng-app="app" class="col-sm-12 sign-in-page">
<div class="row" ng-controller="controllerName">
..... contents of redirected page ....
</div>
</div>
귀하의 WebAPI를 - (가정 그것은 C 번호입니다 웹 API 2.2은 다음과 비슷한 모습이 될 것
[HttpPost]
public string Post([FromBody]string jsonString)
{
try
{
IDictionary<string, string> data = JsonConvert.DeserializeObject<IDictionary<string, string>>(jsonString);
개체의 값은 모두 문자열 가정 ....
이 구현은 이상적이지는 않지만 제대로 작동하지 않을 수 있습니다.
아예 양식 컨트롤이 포함 된 원래보기에 각도 POST를 추가 할 수도 있습니다. 하지만 내 경우에는 View가 전체 게시물을 작성해야하고 전체 게시물의 데이터를 모델에서 처리해야하고 컨트롤러가 모델의 일부 데이터를 가져 와서 세션 정보와 결합하여이를 작성해야하므로 그러면 웹 API 컨트롤러로 보내야합니다.
기본적으로 aC# Web API 클라이언트가 필요합니다. ... 나는 이것을 볼 것입니다 http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client –
RestSharp를 사용하면 수동으로 할 수 있습니다. 미쳤다. – Wjdavis5
컨트롤러 대신 웹 API 엔드 포인트에 직접 전달하지 않는 이유는 무엇입니까? 먼저 컨트롤러에 갈 이유가 무엇입니까? –