Angular [restService] error 400 {“Message”:”请求无效。”}

huangapple go评论79阅读模式
英文:

Angular [restService] error 400 {"Message":"The request is invalid."}

问题

I am trying to display the values in the detailed View on click on the search button. Below is my listView.html code.

<span class="link" ng-click="vm.openDetailsView(firmware.id)"><i class="fa fa-search"></i>&nbsp;View</span>

And respective listView typescript file, where I'm passing firmware.id as a parameter and redirecting to the detail view page.

openDetailsView(fwId: string) {
  this.$window.location.href = `#/firmwareCatalog/firmware/${fwId}`;
}

My detailView.ts file is shown below:

export class FirmwareController {
  isLoading: boolean = false;
  private logger: ILogger;
  private firmwareId: any;
  private firmware: any;

  constructor(
    private $stateParams: any,
    logger: LoggerService,
    private firmwareService: FirmwareService,
    private modalService: ModalService,
    private $window: any
  ) {
    this.reset();
    this.firmwareId = $stateParams.firmwareId;
    this.loadFirmwareDetails();
    this.logger = logger.getLogger('firmware');
  }

  loadFirmwareDetails() {
    this.isLoading = true;
    this.firmwareService.getFirmware(this.firmwareId)
      .then((fw: any) => {
        this.firmware = fw;
      })
      .finally(() => {
        this.isLoading = false;
      });
  }
}

Respective DetailView.service.ts file:

private routes = {
  getFirmwareById: '/firmwares/{:firmwareId}',
};

getFirmware(id: any): any {
  const uri = this.routes.getFirmwareById.replace('{:firmwareId}', id);
  return this.restService
    .get(uri, null, true)
    .then((response: any) => {
      if (response.data) {
        return response.data;
      }
    })
    .catch((response: any) => {
      return this.$q.reject(response);
    });
}

Below is my Controller.cs:

[Route("firmwares/{firmwareId}")]
[HttpGet]
public Firmware GetById(int firmwareId)
{
    this.logService.Debug($"Entering GET api/firmwares/{firmwareId}");

    var connectedUser = this.permissionService.GetConnectedUser();
    this.permissionService.CheckRolePermission(connectedUser.Role, PermissionConstant.ViewFirmwares);

    var result = Firmware.From(this.etpService.GetFirmwarefromEtpById(firmwareId));
    return result;
}

Below is my model class:

public class Firmware
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("deviceType")]
    public string DeviceType { get; set; }

    [JsonProperty("filename")]
    public string Filename { get; set; }

    [JsonProperty("size")]
    public int Size { get; set; }

    [JsonProperty("uploadDate")]
    public DateTime UploadDate { get; set; }

    [JsonProperty("url")]
    public string Url { get; set; }

    [JsonProperty("version")]
    public string Version { get; set; }

    public static EtpFirmwareModel From(EtpFirmwareModel model)
        => model == null ? null : new EtpFirmwareModel
        {
            Id = model.Id,
            DeviceType = model.DeviceType,
            Filename = model.Filename,
            Size = model.Size,
            UploadDate = model.UploadDate,
            Url = model.Url,
            Version = model.Version
        };
}

I am using GET verb in order to get the data from the API and not able to hit my Controller.cs file. Here is the error details:

[restService] {"Message":"The request is invalid."}

I am new to Angular, please help me fix this. Thanks in advance.

英文:

I am trying to display the values in the detailed View on click on search button. Below is my listView.html code.

&lt;span class=&quot;link&quot; ng-click=&quot;vm.openDetailsView(firmware.id)&quot;&gt;&lt;i class=&quot;fa fa-search&quot;&gt;&lt;/i&gt;&amp;nbsp;View&lt;/span&gt;

And respective listView typescript file, where Im passing firmware.id as parameter and redirecting to detail view page.

    openDetailsView(fwId: string) {
      this.$window.location.href = `#/firmwareCatalog/firmware/${fwId}`;
    }

My detailView.ts file shown below:

  export class FirmwareController {
    isLoading: boolean = false;

    private logger: ILogger;

    private firmwareId: any;

    private firmware: any;

    constructor(private $stateParams: any,
      logger: LoggerService,
      private firmwareService: FirmwareService,
      private modalService: ModalService,
      private $window: any) {

      this.reset();

      this.firmwareId = $stateParams.firmwareId;

      this.loadFirmwareDetails();

      this.logger = logger.getLogger(&#39;firmware&#39;);
    }

    loadFirmwareDetails() {
      this.isLoading = true;
      this.firmwareService.getFirmware(this.firmwareId)
        .then((fw: any) =&gt; {
          this.firmware = fw;
        })
        .finally(() =&gt; {
          this.isLoading = false;
        });
    }
  }

respective DetailView.service.ts file:

    private routes = {
      getFirmwareById: &#39;/firmwares/{:firmwareId}&#39;,
    };

getFirmware(id: any): any {
      const uri = this.routes.getFirmwareById.replace(&#39;{:firmwareId}&#39;, id);
      return this.restService
        .get(uri, null, true)
        .then((response: any) =&gt; {
          if (response.data) {
            return response.data;
          }
        })
        .catch((response: any) =&gt; {
          return this.$q.reject(response);
        });
    }

Below is my Controller.cs:

[Route(&quot;firmwares/{firmwareId}&quot;)]
        [HttpGet]
        public Firmware GetById(int firmwareId)
        {
            this.logService.Debug($&quot;Entering GET api/firmwares/{firmwareId}&quot;);

            var connectedUser = this.permissionService.GetConnectedUser();
            this.permissionService.CheckRolePermission(connectedUser.Role, PermissionConstant.ViewFirmwares);

            var result = Firmware.From(this.etpService.GetFirmwarefromEtpById(firmwareId));
            return result;
        }

Below is my model class :

public class Firmware
    {
        [JsonProperty(&quot;id&quot;)]
        public string Id { get; set; }

        [JsonProperty(&quot;deviceType&quot;)]
        public string DeviceType { get; set; }

        [JsonProperty(&quot;filename&quot;)]
        public string Filename { get; set; }

        [JsonProperty(&quot;size&quot;)]
        public int Size { get; set; }

        [JsonProperty(&quot;uploadDate&quot;)]
        public DateTime UploadDate { get; set; }

        [JsonProperty(&quot;url&quot;)]
        public string Url { get; set; }

        [JsonProperty(&quot;version&quot;)]
        public string Version { get; set; }

        public static EtpFirmwareModel From(EtpFirmwareModel model)
                =&gt; model == null ? null : new EtpFirmwareModel
                {
                    Id = model.Id,
                    DeviceType = model.DeviceType,
                    Filename = model.Filename,
                    Size = model.Size,
                    UploadDate = model.UploadDate,
                    Url = model.Url,
                    Version = model.Version
                };
    }

I am using GET verb inorder to get the data from API. and not able to hit to my Controller.cs file Here is the Error details :

[restService] {&quot;Message&quot;:&quot;The request is invalid.&quot;} 

I am new to angular, Please help me to fix this. Thanks in advance.

答案1

得分: 0

"The error message 'The request is invalid' means path is not correct that is written
#/firmwareCatalog/firmware/${fwId}

Please cross check the controller path. I can see the path mentioned in controller that is [Route('firmwares/{firmwareId}')]. so path should be firmwares instead of firmware.

Can you try the below path

this.$window.location.href = /firmwares/${fwId};"

英文:

The error message "The request is invalid" means path is not correct that is written
#/firmwareCatalog/firmware/${fwId}

Please cross check the controller path. I can see the path mentioned in controller that is [Route("firmwares/{firmwareId}")]. so path should be firmwares instead of firmware.

Can you try the below path

this.$window.location.href = /firmwares/${fwId};

huangapple
  • 本文由 发表于 2023年5月11日 18:13:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定