c# - 将值从 Angular 服务应用程序发送到 C# 后端 Controller

标签 c# angular typescript

我对此感到非常疯狂,我几乎尝试了所有方法,但找不到将字符串值从服务传递到后端以基于该字符串返回 Json 结果的方法。

问题是,我有一个后端,它使用元信息来 cooking 所有 JSON 前端提供,然后将它们返回到前端进行显示,在这种情况下,我必须获得一个基于过滤器的 JSON,该过滤器是由插入到前端的字符串创建的,但找不到将字符串传递到后端的方法,并且我不想通过 URL 传递它。

这是我的代码:

Angular typescript 服务:我想传递“whr”

getAdvancedFilterResult(concept: string, whr: string): Promise<any> {

  const headers: Headers = new Headers({
    'Content-Type': 'application/json'
  });
  this.authService.getAuthorizationHeader(headers);
  headers.append('IdLng', this.config.idLanguage);

  const options: RequestOptions = new RequestOptions();
  options.headers = headers;

  return this.http.get(this.config.apiDomain + this.config.apiEndpointEntities + '/' + concept + '/' + "filtered",
      options
    )
    .toPromise()
    .then(
      response => response.json() as any[]
    )
    .catch((error) => this.customHandleError(error, this.toastrService));
}

后端 Controller :

[Route("api/Entities/{entity}/filtered/")]
public HttpResponseMessage GetFilter(string entity) {

  HttpResponseMessage response = new HttpResponseMessage();
  string action = "READ";

  //Check Authorization
  AuthorizationResponse authResponse = AuthProvider.CheckAuthorization(new AuthorizationRequest() {
    SCode = UserUtils.GetUserSCode(User),
      ConceptString = entity,
      ActionString = action,
      UserId = UserUtils.GetUserID(User),
      ExtraParameters = new AuthorizationRequest.ExtraParamaters() {
        IdsOnly = false, Where = "!!!!!WHR HERE!!!!"
      }
  });
  if (authResponse.IsAuthorized) {
    //code
    response = Request.CreateResponse(HttpStatusCode.OK, json);
  } else {
    response = Request.CreateResponse(HttpStatusCode.Unauthorized);
  }
  return response;
}

我应该通过 header 传递它,并使用 headers.append('whr', whr); 进入 optionshttp.get 上还是通过 options.body = whr; 进入 body

另外,如何在后端使用它?

最佳答案

您应该像这样传递 header :

getAdvancedFilterResult(concept: string, whr: string): Promise<any> { 
  this.authService.getAuthorizationHeader(headers);
  let headers: HttpHeaders = new HttpHeaders();
  headers = headers.append('Content-Type', 'application/json');
  headers = headers.append('x-corralation-id', '12345');
  headers = headers.append('IdLng', this.config.idLanguage);
  headers = headers.append('whr', whr);

  const options: RequestOptions = new RequestOptions();
  options.headers = headers;

  return this.http.get(this.config.apiDomain + this.config.apiEndpointEntities + '/' + concept + '/' + "filtered",
      options
    )
    .toPromise()
    .then(
      response => response.json() as any[]
    )
    .catch((error) => this.customHandleError(error, this.toastrService));
}

要在服务器端获取 header ,请尝试以下操作:

[Route("api/Entities/{entity}/filtered/")]
public HttpResponseMessage GetFilter(string entity) {

  var request = Request;
  var headers = response.Headers;
  HttpResponseMessage response = new HttpResponseMessage();
  string action = "READ";
  var whrHeader = headers.Contains("whr") ? request.Headers.GetValues("whr").First() : ""

  AuthorizationResponse authResponse = AuthProvider.CheckAuthorization(new AuthorizationRequest() {
    SCode = UserUtils.GetUserSCode(User),
      ConceptString = entity,
      ActionString = action,
      UserId = UserUtils.GetUserID(User),
      ExtraParameters = new AuthorizationRequest.ExtraParamaters() {
        IdsOnly = false,
        Where = whrHeader
      }
  });
  if (authResponse.IsAuthorized) {
    //code
    response = Request.CreateResponse(HttpStatusCode.OK, json);
  } else {
    response = Request.CreateResponse(HttpStatusCode.Unauthorized);
  }
  return response;
}

关于c# - 将值从 Angular 服务应用程序发送到 C# 后端 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53068553/

相关文章:

c# - 对同一 WCF 服务的不同请求的不同超时

typescript - 使用注入(inject)的 Angular 服务作为静态方法中的依赖项

javascript - 如何使用 linq-es2015 在两个键上进行 GroupBy?

以字符串属性作为类型指示符的 json 对象的 Typescript 定义文件

typescript - deno 推荐使用哪种构建工具?

html - 将文件输入 FileReader base64 结果动态分配给 IMG SRC

c# - 保护 asp.net webform cookie 不起作用

c# - 使用 Rhino Mocks 变干

c# - 将 IPagedList 与复选框绑定(bind)为列表

css - 如何更改 Angular 6 Material 单选按钮外部波纹颜色