简体   繁体   English

我无法访问我在现有项目中创建的新 Web API controller

[英]I can't access a new Web API controller that I've created in an existing project

In an existing ASP.NET MVC project, I've created a new Web API controller.在现有的 ASP.NET MVC 项目中,我创建了一个新的 Web API Z594C103F2C6E04C0CCE8Z API Z594C103F29031E。

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);
            }
        }
    }
}

When I'm trying to access it via browser using:当我尝试通过浏览器访问它时,使用:

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

I get the error:我得到错误:

Server Error in '/'Application. “/”应用程序中的服务器错误。 The resource cannot be found.无法找到该资源。

My RouteConfig.cs contains:我的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 }
        );
    }
}

The WebApiConfig.cs file contains: 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 }
        );
    }
}

The existing controllers are working properly.现有的控制器工作正常。

Can you please change the action signature as [HttpGet] public Call Get(int id) and access as localhost:53366/api/Tablet/157您能否将操作签名更改为 [HttpGet] public Call Get(int id) 并作为 localhost:53366/api/Tablet/157 访问

When you encounter any serialization issues then Make sure both the serialization at "Call" model and "httpclient" in sync.当您遇到任何序列化问题时,请确保“Call”model 和“httpclient”处的序列化同步。

Please find more details about 'ObjectContent`1': Failed to serialize the response from DbSet请查找有关“ObjectContent`1”的更多详细信息:无法序列化来自 DbSet 的响应

Try these two options:试试这两个选项:

Option 1. Use following URL:选项 1. 使用以下 URL:

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

Option 2. Modify code like:选项 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);
        }
    }
}

and then try to call it with following URL:然后尝试使用以下 URL 调用它:

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

OR或者

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

In {controller}/{action}/{id} , {action} is the method name, which in your controller is Get .{controller}/{action}/{id}中, {action}是方法名称,在您的 controller 中是Get In your url ( http://localhost:53366/Tablet/Call/157 ), you have Call , which is the return type of your method.在您的 url ( http://localhost:53366/Tablet/Call/157 )中,您有Call ,这是您的方法的返回类型。 Try navigating to http://localhost:53366/Tablet/Get/157尝试导航到http://localhost:53366/Tablet/Get/157

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

if still not work keep add [HttpGet] like below如果仍然无法正常工作,请继续添加 [HttpGet],如下所示

[HttpGet] public Call Get(int call_id) [HttpGet] 公共调用获取(int call_id)

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

相关问题 WEB API 2控制器无法访问资源 - WEB API 2 controller can't access the resource 无法访问我刚刚创建的课程 - Can't access the class I just created 我创建了一个无限循环,但我不知道为什么 - I've created an infinite loop and I don't know why 我可以(或应该)使用 IAsyncEnumerable<T> 而不是任务<ActionResult<IEnumerable<T> &gt;&gt; 在 Web API 控制器中 - Can (or should) I use IAsyncEnumerable<T> instead of Task<ActionResult<IEnumerable<T>>> in a Web API Controller 如何定位使用for循环创建的特定按钮? - How can i target a specific button that i've created with a for loop? 我可以听我创建的Windows服务事件吗? - Can I listen for a Windows service event that I've created? 如何序列化我创建的类的 List&lt;&gt; - How can I serialize a List<> of classes that I've created 我可以从Web API访问IIdentity吗? - Can I access IIdentity from Web API 我似乎无法访问我在 Nuget package 安装程序中安装的软件包 - I can't seem to access packages I've installed inside the Nuget package installer for the solution 我看不到我在项目中创建的文件夹 - I can't see the folders I created in my project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM