[英]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.