![](/img/trans.png)
[英]ASP.NET CORE 2.2 New Project, Failed to load resource: the server responded with a status of 404 (Not Found) error in server for JS/CSS/Image
[英]Error: Request failed with status code 404 in ASP.NET Core with Vue.js
index.cshtmp
页面中只有一个按钮和脚本。 在我的 main.Js 中,我有一个名为 getItems() 的方法用于检索所有项目。 我为此使用了 Vue.js 和 Axios。 我在Admincontroller
调用了getItems
方法。这是一个 GET 请求。 但它给了我控制台错误Error: Request failed with status code 404
。 我在 Admin 控制器中添加了一个刹车点,但我的请求没有进入控制器。 请帮我解决这个问题。谢谢
**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>
}
主文件
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;
})
}
}
});
管理控制器
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));
}
}
我在 Admincontroller 中调用了 getItems 方法。这是一个 GET 请求。 但它给了我控制台错误错误:请求失败,状态代码为 404。我在管理控制器中添加了一个制动点,但我的请求没有进入控制器。
如果您在同一个项目中混合使用 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.