Webform内的ASP.NET ApiController无法访问方法 [英] ASP.NET ApiController inside a webform can't reach methods

查看:537
本文介绍了Webform内的ASP.NET ApiController无法访问方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何我都无法从ApiController到达任何方法,如果我尝试通过浏览器到达它但没有显示任何方法,路由就会出现.

I can't reach any methods from my ApiController in anyway, the routing does appear if i try to reach it by a browser but no methods are shown.

我的控制器:

namespace AgroRiego.Controllers
{
    public class datacontrol : ApiController
    {
        [HttpGet, Route("api/get")]
        public string Get([FromUri]string user, string pass)
        {
            string check = SQL.Reader("SELECT * FROM users WHERE username='" + user + "' AND password='" + pass + "'");
            if (String.IsNullOrWhiteSpace(check))
            {
                return "error en credenciales";
            }
            DataTable horarios = SQL.table_read("SELECT * FROM horario_riego");
            string json = Utils.ConvertDataTabletoJSON(horarios);

            return json;
        }

        [HttpPost, Route("api/post")]
        public void Post([FromBody]string value)
        {
            string i = value;
        }
    }
}

我的全球Asax:

namespace AgroRiego
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);
        }
    }
}

和我的webapiconfig:

and my webapiconfig:

namespace AgroRiego
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Configuración y servicios de API web

            // Rutas de API web
            config.MapHttpAttributeRoutes();

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

我在项目中有更多的Web表单(最初只是带有服务器端代码的html页面,但是我需要添加一些方法来检索和发送数据,非常感谢!

i have more webforms inside the project (originally it was just html pages with serverside code, but i need to add a couple methods to retrieve and send data, help much appreciated!

我设法通过更改URL到达HTTP 200,但是无论如何我都无法到达方法(在调试模式下它不会在断点处停止)如何正确路由Api(因此它不是Login.aspx) ),以及如何解决到达的方法?

i managed to reach HTTP 200 changing the URL but i can't reach the methods anyway (in debug mode it does not stop on the breakpoints) how can i route correctly the Api (so it is not Login.aspx) and how do i fix the methods reaching?

我在文档中读到了我需要在全局中使用此行:

i read in documentation that i need this line in global:

RouteConfig.RegisterRoutes(RouteTable.Routes);

但是我不使用MVC没关系吗?我尝试使用全新的MVC Web Api到达路线,但结果为无响应"

but im not using MVC does that matter? i tried reaching the routes with a brand new MVC Web Api and it yields "No Response"

推荐答案

在控制器上使用routerprefix.因此,您以

use a routerprefix with your controller. So you access the URL as


    http://localhost/routerprefix/router

HttpClient类可用于发送和接收HTTP请求和响应.由于您尝试从aspx页面使用WebApi,因此更好的方法是创建HttpClient实例

HttpClient class can be use to send and receive HTTP requests and responses. Since you are trying to consume a WebApi from a aspx page, better way is to create a HttpClient instance

下面是一个非常简单的实现.请检查此网址以获取更多信息

Below is a very simple implementation. Please check this url for further information

MSDN示例



    HttpClient client = new HttpClient();

    HttpResponseMessage response = await client.GetAsync("http://localhost:49342/api/get");
    if (response.IsSuccessStatusCode)
    {
        product = await response.Content.ReadAsAsync();
    }

这篇关于Webform内的ASP.NET ApiController无法访问方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆