Skip to main content
 首页 » 编程设计

c#之ASP MVC WebApi 调用返回错误 'not found'

2024年02月05日29mayingbao

"<!DOCTYPE html> 
<html> 
    <head> 
        <title>The resource cannot be found.</title> 
        <meta name="viewport" content="width=device-width" /> 
        <style> 
         body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}  
         p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px} 
         b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px} 
         H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red } 
         H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon } 
         pre {font-family:"Consolas","Lucida Console",Monospace;font-size:11pt;margin:0;padding:0.5em;line-height:14pt} 
         .marker {font-weight: bold; color: black;text-decoration: none;} 
         .version {color: gray;} 
         .error {margin-bottom: 10px;} 
         .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; } 
         @media screen and (max-width: 639px) { 
          pre { width: 440px; overflow: auto; white-space: pre-wrap; word-wrap: break-word; } 
         } 
         @media screen and (max-width: 479px) { 
          pre { width: 280px; } 
         } 
        </style> 
    </head> 
 
    <body bgcolor="white"> 
 
            <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1> 
 
            <h2> <i>The resource cannot be found.</i> </h2></span> 
 
            <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif "> 
 
            <b> Description: </b>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. &nbsp;Please review the following URL and make sure that it is spelled correctly. 
            <br><br> 
 
            <b> Requested URL: </b>/api/ComicApi/1<br><br> 
 
            <hr width=100% size=1 color=silver> 
 
            <b>Version Information:</b>&nbsp;Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1637.0 
 
            </font> 
 
    </body> 
</html> 
<!--  
[HttpException]: The controller for path &#39;/api/ComicApi/1&#39; was not found or does not implement IController. 
   at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) 
   at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) 
   at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) 
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) 
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) 
   at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) 
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 
-->" 

My JQuery / javascript

$.ajax({ 
 
    url: "/api/ComicApi/" + 1, 
    //data: { name: productName }, 
    type: "GET", 
    dataType: "jsonp", 
    error: function (request, status, error) { 
        alert('error'); 
    }, 
    success: function (data) { 
        alert('success'); 
    } 
}); 

Global.asax

protected void Application_Start() 
{ 
    AreaRegistration.RegisterAllAreas(); 
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
    RouteConfig.RegisterRoutes(RouteTable.Routes); 
    BundleConfig.RegisterBundles(BundleTable.Bundles); 
    GlobalConfiguration.Configure(WebApiConfig.Register);//added this 
} 

WebApi.Config

class WebApiConfig 
{ 
    public static void Register(HttpConfiguration configuration) 
    { 
        configuration.Routes.MapHttpRoute("API Default", "api/{controller}/{id}", 
            new { id = RouteParameter.Optional }); 
    } 
} 

ComicApiController.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Web.Http; 
 
namespace ComicNet.WebApi 
{ 
    public class ComicApiController : ApiController 
    { 
        // GET api/<controller> 
        public IEnumerable<string> Get() 
        { 
            return new string[] { "value1", "value2" }; 
        } 
 
        // GET api/<controller>/5 
        public string Get(int id) 
        { 
            return "value"; 
        } 
 
        // POST api/<controller> 
        public void Post([FromBody]string value) 
        { 
        } 
 
        // PUT api/<controller>/5 
        public void Put(int id, [FromBody]string value) 
        { 
        } 
 
        // DELETE api/<controller>/5 
        public void Delete(int id) 
        { 
        } 
    } 
} 

请您参考如下方法:

Web API路由需要在MVC路由之前注册

protected void Application_Start() { 
    AreaRegistration.RegisterAllAreas(); 
    GlobalConfiguration.Configure(WebApiConfig.Register);//moved higher up 
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
    RouteConfig.RegisterRoutes(RouteTable.Routes); 
    BundleConfig.RegisterBundles(BundleTable.Bundles); 
}