Only a button and scripts are in the index.cshtmp
page. In my main.Js, I have a method called getItems() for retrieving all the items. I used Vue.js and Axios for that. I called getItems
methods inside a Admincontroller
.It is a GET request. 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. 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. 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.
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.
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?}");
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.