繁体   English   中英

在mvc asp.net的浏览器中找不到API控制器

[英]API controller can't be found in browser in mvc asp.net

我是single page applicationwebapi 。我创建了一个webapi如下所示:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using WebApplication6.Models;

namespace WebApplication6.Controllers
{
    public class ManageStudentInforAPIController : ApiController
    {
        private SchoolManagementEntities db = new SchoolManagementEntities();

        // GET: api/ManageStudentsInfoAPI  
        public IQueryable<Student> Get()
        {
            return db.Students;
        }
    }
}

但是当我将此URL称为http://localhost:5411/ManageStudentInforAPI ,结果是

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /ManageStudentInforAPI

我的问题在哪里。您需要的任何详细信息都可以发布。我正在使用vs2015

在此处输入图片说明

您尝试将控制器作为http:// localhost:5411 / Api / ManageStudentInforAPI调用,这应该可以工作

您在ManageStudentInforAPI之前错过了api 尝试用http://localhost:5411/api/ManageStudentInforAPI替换您的URL

或者,您可以将[Route("route")]到控制器或方法中,并调用http://localhost:5411/api/route

像这样调用您的api http://localhost:5411/api/ManageStudentInforAPI

或者,如果您想要自定义api网址,则可以这样设置路由

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using WebApplication6.Models;

namespace WebApplication6.Controllers
{
    public class ManageStudentInforAPIController : ApiController
    {
        private SchoolManagementEntities db = new SchoolManagementEntities();

        // GET: api/ManageStudentsInfoAPI
        [Route("api/MyApi")]
        public IQueryable<Student> Get()
        {
            return db.Students;
        }
    }
}

然后像这样调用您的api http://localhost:5411/api/MyApi

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM