如何在blazor webassembly应用程序(客户端)中调用blazor服务器应用程序(登录webapi)

时间:2020-08-29 13:35:39

标签: c# blazor blazor-server-side blazor-client-side blazor-webassembly

首先,我创建blazor服务器应用程序并创建登录webapi,然后添加一个blazor webassembly应用程序项目以调用Webapi

blazor服务器应用项目:

查看登录webapi blazor服务器应用程序的响应

enter image description here

blazor webassembly应用项目:

EmpsController.cs

        [HttpPost]
        public Emp checkLogin(string username, string password)
        {
            Emp hasemp = _context.emps.Where(e => e.username == username & e.password == password).FirstOrDefault();
            return hasemp;
        }

Index.razor


@page "/"
@using CrudBlazorServerApp.Data
@using System.Net.Http.Headers;
<h3>Login</h3>
@inject HttpClient Http

<div>
    <input id="txtusername" type="text" placeholder="Enter Your UserName" /><br />
    <input id="txtpassword" type="text" placeholder="Enter Your Password" /><br />
    <input type="submit" />


    @if (empList.username == "" || empList.username == null)
    {
        var data = new
        {
            message = "Enter Username ",
        };
    }
    else if (empList.password == "" || empList.password == null)
    {
        var data = new
        {
            message = "Enter Password",
        };
    }
    else
    {
        @if(empList.username == "txtusername" || empList.username == "txtpassword") //here I am trying to check the when user enter the valid username and password
        {
            var data = new
            {
                message = "Success",
                data = new { empList.username, empList.password }
            };
            //if username and password correct than redirect the registraion page
        }
        else
        {

            var data = new
            {
                message = "Username and Password incorrect ",
            };
        }
    }
</div>

@code{
                Emp empList=new Emp();
    protected override async Task OnInitializedAsync() =>
        empList = await Http.GetFromJsonAsync<Emp>("https://localhost:44333/api/emps/checkLogin?username=" + empList.username + "&password=" + empList.password);
}  

查看错误图片:

enter image description here

查看我的数据库,我只有一条记录:

enter image description here

项目层次结构:

enter image description here

我的webapi在服务器端工作正常,但现在我尝试在客户端调用登录webapi

这是错误:

 Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Failed to compare two elements in the array.
System.InvalidOperationException: Failed to compare two elements in the array.  
---> System.InvalidOperationException: The following routes are ambiguous:

'DisplayEmployeeData' in 'CrudBlazorClientApp.Pages.Login'

'DisplayEmployeeData' in 'CrudBlazorClientApp.Pages.DisplayEmployeeData'


  at Microsoft.AspNetCore.Components.RouteTableFactory.RouteComparison (Microsoft.AspNetCore.Components.Routing.RouteEntry x, Microsoft.AspNetCore.Components.Routing.RouteEntry y) <0x2f1d0b8 + 0x0025e> in <filename unknown>:0 
  at System.Collections.Generic.ComparisonComparer`1[T].Compare (T x, T y) <0x2f1d078 + 0x00012> in <filename unknown>:0 
  at System.Linq.EnumerableSorter`2[TElement,TKey].CompareAnyKeys (System.Int32 index1, System.Int32 index2) <0x2f1cf40 + 0x00024> in <filename unknown>:0 
  at System.Collections.Generic.ComparisonComparer`1[T].Compare (T x, T y) <0x2f1ce80 + 0x00012> in <filename unknown>:0 
  at System.Collections.Generic.ArraySortHelper`1[T].InsertionSort (T[] keys, System.Int32 lo, System.Int32 hi, System.Comparison`1[T] comparer) <0x2f1cdc0 + 0x00064> in <filename unknown>:0 
  at System.Collections.Generic.ArraySortHelper`1[T].IntroSort (T[] keys, System.Int32 lo, System.Int32 hi, System.Int32 depthLimit, System.Comparison`1[T] comparer) <0x2f1cc58 + 0x000ae> in <filename unknown>:0 
  at System.Collections.Generic.ArraySortHelper`1[T].IntrospectiveSort (T[] keys, System.Int32 left, System.Int32 length, System.Comparison`1[T] comparer) <0x2f1c9d0 + 0x00032> in <filename unknown>:0 

main issue is here,当用户在数据库中输入正确或错误的值时,我需要比较哪个值?我正在同时检查“ txtusername”和txtusername.Text

@if(empList.username == "txtusername" || empList.username == "txtpassword") //here I am trying to check the when user enter the valid username and password
        {

1 个答案:

答案 0 :(得分:0)

控制器操作具有httpost属性,您可以使用get(GetFromJsonAsync)调用它吗?该属性必须为HttpGet。