c# - 错误:使用 Vue.js 在 ASP.NET Core 中请求失败,状态码为 404
问题描述
页面中只有一个按钮和脚本index.cshtmp
。在我的 main.Js 中,我有一个名为 getItems() 的方法,用于检索所有项目。为此,我使用了 Vue.js 和 Axios。我getItems
在 .It 中调用了方法Admincontroller
,这是一个 GET 请求。但它给了我控制台错误 Error: Request failed with status code 404
。我在管理控制器中添加了一个刹车点,但我的请求没有进入控制器内部。请帮我解决这个问题。谢谢
**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;
})
}
}
});
管理员控制器
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?}");
});
}
推荐阅读
- php - Trying to sort by price but not working in WordPress
- ruby-on-rails - 添加 pg_dump 后运行 rails 服务器时 main:Object (NoMethodError) 的未定义方法“命名空间”
- css - ReactJS:如何将浮动元素 -textB- 和初始放置的文本 -textA- 放在同一基线上?
- python - 如何从模型预测中获取 GMM 聚类信息?
- mongodb - 使用邮递员 + 快递时 PUT/PATCH 未通过
- azure - Azure 应用程序网关不允许客户端证书
- java - 使用浮点数组和布尔值作为参数编写布尔方法
- angular - 如何将 CORS 标头添加到 401/403 响应?
- python - 如何使用 tkinter 更改下拉菜单的位置
- python - 如何在 QDateEdit 弹出 QCalendarWidget 中添加今天按钮