简体   繁体   English

值未从 service-proxies.ts 文件传递到 Appservices.cs

[英]Value is not passing from service-proxies.ts file to Appservices.cs

I am trying to fetch email id value from text box and want to send it in api method in cs file.我正在尝试从文本框中获取 email id 值,并希望在 cs 文件中以 api 方法发送它。 But it is not working, I have tried below code..can any one help me.但它不起作用,我尝试了下面的代码..任何人都可以帮助我。

Value is successfully coming in service file, I have alert that but not going in cs file.价值已成功进入服务文件,我有警报但没有进入 cs 文件。

I am using Angular in front end and.Net core in backend API.我在前端使用 Angular,在后端使用 .Net 核心 API。 Also this is Aspnetzero project.这也是 Aspnetzero 项目。

First of all below is register.component.html file code:首先下面是register.component.html文件代码:

<div class="form-group">
                <input #emailAddressInput="ngModel" 
                       placeholder="{{'EmailAddress' | localize}} *" 
                    [(ngModel)]="model.emailAddress" name="EmailAddress" required maxlength="256"/>
            </div>

            <div class="pb-lg-0 pb-5">
                <button type="button" class="btn btn-primary blue float-right"
                 (click)="checkUserExist(model.emailAddress)">Continue</button>
            </div>

Below is code from register.component.ts file下面是 register.component.ts 文件中的代码

checkUserExist(emailid): void {
        this._accountService.registerHostLevelUser(emailid).subscribe((result) => {
            alert(result);
        });
    }

Below is code of my function in service-proxies.ts file.下面是我在 service-proxies.ts 文件中的 function 的代码。

registerHostLevelUser(value:any){
        let url_ = this.baseUrl + "/api/services/app/Account/RegisterHostLevelUser";
        url_ = url_.replace(/[?&]$/, "");

        return this.http.post(url_,value);
      }

Below is code from AccountAppServices.cs file下面是来自 AccountAppServices.cs 文件的代码

 public async Task<string> RegisterHostLevelUser(string value)
        {
            Debug.WriteLine(value); // here i am trying to write value but it is alwasy coming null.

            return "Done";
        }

On post methods the controller expects a JSON object to be passed as parameter, you are trying to bind a single string, wich is not working because a string is not a JSON object. On post methods the controller expects a JSON object to be passed as parameter, you are trying to bind a single string, wich is not working because a string is not a JSON object.

Try this:尝试这个:

[Route("{args}")]
public async Task<string> myMethod([FromRoute] string args) {}

and change this part as well并更改这部分

registerHostLevelUser(value:any) {
    let url_ = this.baseUrl + "/api/services/app/Account/RegisterHostLevelUser/" + value;
    url_ = url_.replace(/[?&]$/, "");
    return this.http.post(url_,null);
}

As well you can build and object with a single string property and pass the object as parameter as an JSON body to your controller, but the way above its more pratical. As well you can build and object with a single string property and pass the object as parameter as an JSON body to your controller, but the way above its more pratical.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM