【问题标题】:send value from Angular service App to C# backend controller将值从 Angular 服务应用程序发送到 C# 后端控制器
【发布时间】:2019-04-03 17:53:48
【问题描述】:

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

这就是问题所在,我有一个使用元信息来处理所有 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));
}

后端控制器:

[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;
}

我应该通过headerheaders.append('whr', whr); 将它传递到http.get 上的options 还是通过options.body = whr; 进入body

另外,我怎样才能让它在后端使用?

【问题讨论】:

    标签: c# angular typescript


    【解决方案1】:

    您应该像这样传递标头:

    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));
    }
    

    要获取服务器端的标头,请尝试以下操作:

    [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;
    }
    

    【讨论】:

    • 我怎样才能得到带有我想在后端捕获的字符串的标题?因为我需要将“whr”字符串注入后端的额外参数
    【解决方案2】:

    感谢 SiddAjmera,我得到了解决方案!

    在前端服务:

    getAdvancedFilterResult(concept: string, whr: string): Promise<any> {
      let headers: Headers = new Headers();
      this.authService.getAuthorizationHeader(headers);
      headers.append('Content-Type', 'application/json');
      headers.append('IdLng', this.config.idLanguage);
      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));
    }
    

    然后在后端,只需使用已经创建的 UserUtils 来获取具有值 'whr' 的标头并将其传递给函数。

    UserUtilis.cs:

    public static string Where(HttpRequestMessage re) {
      string whereCLause = "";
    
      var headers = re.Headers;
      if (headers.Contains("whr")) {
        whereCLause = headers.GetValues("whr").First();
      } else {
        whereCLause = " ";
      }
      return whereCLause;
    }
    

    还有Controller.cs

    ...
    var re = Request;
    
    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 = UserUtils.Where(re)
        }
    });
    ...
    

    【讨论】:

      猜你喜欢
      • 2015-02-15
      • 2018-05-04
      • 2018-09-16
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      • 2019-04-19
      相关资源
      最近更新 更多