hi,我想以角度显示URL地址中的数据,但是运行程序时出现此错误
InvalidOperationException:找不到视图“索引”。搜索了以下位置:/Views/Home/Index.cshtml /Views/Shared/Index.cshtml /Pages/Shared/Index.cshtml
这是我编写的代码
public class CustomerController: ControllerBase
{
private readonly ICustomerService _service;
public CustomerController(ICustomerService service)
{
_service = service;
}
[HttpGet]
[Authorize]
[Route("api/customer/getcustomer")]
public Task<CustomerDto> GetCustomer()
{
return _service.GetCustomer();
}
}
并成角度
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class CustomerService {
constructor(private http: HttpClient) {
}
getCustomers() {
return this.http.get('/customer/getcustomer');
//return this.http.get('http://localhost:44391/customer/getcustomer');
}
}
数据显示在URL中,但是我不能以角度格式显示它们 我该怎么办?
答案 0 :(得分:0)
我假设您的Global.asax.cs
中有以下内容
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
尝试添加以下内容
routes.MapRoute(
"GetCustomers", // Route name
"customer/getcustomer", // URL with parameters
new { controller = "Customer", action = "GetCustomer"} // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);