【问题标题】:I can't access a new Web API controller that I've created in an existing project我无法访问在现有项目中创建的新 Web API 控制器
【发布时间】:2020-05-08 20:23:57
【问题描述】:

在现有的 ASP.NET MVC 项目中,我创建了一个新的 Web API 控制器。

namespace EMSMVC.Controllers
{
    public class TabletController : ApiController
    {
        public Call Get(int call_id)
        {
            using(EMSMVCEntities entities = new EMSMVCEntities())
            {
                return entities.Calls.FirstOrDefault(e => e.call_id == call_id);
            }
        }
    }
}

当我尝试通过浏览器访问它时:

http://localhost:53366/Tablet/Call/157

我得到错误:

“/”应用程序中的服务器错误。找不到资源。

我的RouteConfig.cs 包含:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Dashboard", action = "Index", id = UrlParameter.Optional }
        );
    }
}

WebApiConfig.cs 文件包含:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

现有的控制器工作正常。

【问题讨论】:

  • 不应该是http://localhost:53366/api/Tablet/Get/157
  • @Rahul 它不起作用。
  • 您能否将操作签名更改为 [HttpGet] public Call Get(int id) 并访问为localhost:53366/api/Tablet/157
  • @BhasyakaruluKottakota 我似乎正在工作。但是现在我收到错误消息The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.
  • 这是一个序列化问题。确保“Call”模型和“httpclient”的序列化同步。

标签: c# .net asp.net-mvc


【解决方案1】:

您能否将操作签名更改为 [HttpGet] public Call Get(int id) 并作为 localhost:53366/api/Tablet/157 访问

当您遇到任何序列化问题时,请确保“Call”模型和“httpclient”的序列化同步。

请了解更多关于'ObjectContent`1': Failed to serialize the response from DbSet的详细信息

【讨论】:

    【解决方案2】:

    试试这两个选项:

    选项 1. 使用以下网址:

    http://localhost:53366/Tablet/Call?call_id=157
    

    选项2.修改代码如下:

    [RoutePrefix("tablet/services")]
    public class TabletController : ApiController
    {
    
        [Route("getcall")]
        [HttpGet, ActionName("getcall")]
        public Call Get(int call_id)
        {
            using (EMSMVCEntities entities = new EMSMVCEntities())
            {
                return entities.Calls.FirstOrDefault(e => e.call_id == call_id);
            }
        }
    }
    

    然后尝试使用以下 URL 调用它:

    http://localhost:53366/tablet/services/getcall/157
    

    http://localhost:53366/tablet/services/getcall?call_id=157
    

    【讨论】:

    • 谢谢你! http://localhost:53366/tablet/services/getcall?call_id=157 选项正在工作。我正在使用[HttpGet, ActionName("getcall")] 而不是[HttpPost, ActionName("getcall")]
    • 谢谢@zinon,我也更新了答案。
    【解决方案3】:

    {controller}/{action}/{id} 中,{action} 是方法名称,在您的控制器中是Get。在您的 url (http://localhost:53366/Tablet/Call/157) 中,您有 Call,这是您的方法的返回类型。尝试导航到http://localhost:53366/Tablet/Get/157

    【讨论】:

    • 谢谢!我试过了,没有运气。我仍然得到同样的错误。
    【解决方案4】:

    请尝试http://localhost:53366/Tablet/Get/157

    如果仍然无法正常工作,请继续添加 [HttpGet],如下所示

    [HttpGet] public Call Get(int call_id)

    【讨论】:

    • 停止 IIS Express 一次然后请在调试模式下尝试此操作 localhost:53366/api/Tablet/Get/157,
    • 我试过了,我得到了HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
    • Controller 中的呼叫可能是问题所在?它指的是一类模型。
    • [HttpGet] public returntype methodnameOne (int _id) { return (“some Text”); //我考虑返回类型为“字符串” }
    • 好的,谢谢!我需要返回一个数据库表的字段。
    猜你喜欢
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 2016-11-02
    相关资源
    最近更新 更多