[英]Error: Request failed with status code 404 in ASP.NET Core with Vue.js
Only a button and scripts are in the index.cshtmp
page. index.cshtmp
页面中只有一个按钮和脚本。 In my main.Js, I have a method called getItems() for retrieving all the items.在我的 main.Js 中,我有一个名为 getItems() 的方法用于检索所有项目。 I used Vue.js and Axios for that.
我为此使用了 Vue.js 和 Axios。 I called
getItems
methods inside a Admincontroller
.It is a GET request.我在
Admincontroller
调用了getItems
方法。这是一个 GET 请求。 But it gives me that console error Error: Request failed with status code 404
.但它给了我控制台错误
Error: Request failed with status code 404
。 I add a brake point in the Admin controller, But my request does not go inside the controller.我在 Admin 控制器中添加了一个刹车点,但我的请求没有进入控制器。 Please help me to fix this.Thank you
请帮我解决这个问题。谢谢
**index.cshtml**
<div id="app">
<button v-on:click="getItems">Get Items</button>
</div>
@section scripts{
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="~/js/Admin/main.js"></script>
}
main.js主文件
var app = new Vue({
el: '#app',
data: {
price: 0,
showPrice:true,
loading: false
},
methods: {
getItems() {
this.loading = true;
axios.get('/Admin/Items')
.then(res => {
console.log(res);
})
.catch(err =>{
console.log(err);
})
.then(() => {
this.loading = false;
})
}
}
});
AdminController管理控制器
using Microsoft.AspNetCore.Mvc;
using PracticalTest.Application.Admin;
using PracticalTest.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PracticalTest.UIs.Controllers
{
[Route("[controller]")]
public class AdminController : Controller
{
private ApplicationDbContext _ctx;
public AdminController(ApplicationDbContext ctx)
{
_ctx = ctx;
}
[HttpGet("Items")]
public IActionResult GetItems() =>Ok( new GetItems(_ctx).Do());
[HttpPost("Items")]
public IActionResult CreateItem(CreateItem.ItemViewModel vm) =>Ok( new CreateItem(_ctx).Do(vm));
[HttpDelete("Items/{id}")]
public IActionResult DeleteItem(int id) =>Ok( new DeleteItem(_ctx).Do(id));
[HttpPut("Items")]
public IActionResult UpdateItem(UpdateItem.ItemViewModel vm) =>Ok( new UpdateItem(_ctx).Do(vm));
}
}
I called getItems methods inside a Admincontroller.It is a GET request.
我在 Admincontroller 中调用了 getItems 方法。这是一个 GET 请求。 But it gives me that console error Error: Request failed with status code 404. I add a brake point in the Admin controller, But my request does not go inside the controller.
但它给了我控制台错误错误:请求失败,状态代码为 404。我在管理控制器中添加了一个制动点,但我的请求没有进入控制器。
If you mix Razor pages and MVC (or APIs) in same project , please make sure you register required service(s), and required middleware(s) are added.如果您在同一个项目中混合使用 Razor 页面和 MVC(或 API) ,请确保您注册了所需的服务,并添加了所需的中间件。
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddControllers();
//if mix MVC feature, need regiester this service
//services.AddControllersWithViews();
//...
//other sevices you need
//...
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//...
//other required middleware(s) here
//...
app.UseRouting();
//...
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
//endpoints.MapControllerRoute(
// name: "default",
// pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.