【问题标题】:mvc api does not reach Controller get methodmvc api 没有到达 Controller get 方法
【发布时间】:2019-10-15 17:09:57
【问题描述】:

我有一个用 MVC 4 编写的简单 api,当我运行它时写入地址,它没有到达控制器:

这是我的控制器:

 public   IEnumerable<tenMinsStatcs> Get()
    {

       string id="192.168.39.32";
        string dttimeFrom="05082019";
        string dttimeTo="08082019";
        string format = "ddMMyyyy";
        DateTime fromdate = DateTime.ParseExact(dttimeFrom, format, CultureInfo.InvariantCulture);
        DateTime todate = DateTime.ParseExact(dttimeTo, format, CultureInfo.InvariantCulture);


        TestClasscs ts = new TestClasscs();
        ts.m_turbine_id = IPAddress.Parse("192.168.39.82");
        ts.m_time_stamp = Convert.ToDateTime("2019-08-07 5:20:30");
        ts.m_wind_speed = 5;
        ts.norm_wind_max = 3;
        ts.norm_wind_min = 2;
        ts.norm_wind_speed = 3;
        ts.norm_wind_speed_without_ntf = 1;


        List<TestClasscs> myTur = new List<TestClasscs>();
        myTur.Add(ts);

        // mm.m_time_stamp = Convert.ToDateTime("2019-08-07");
        //// mm.m_turbine_id = "192.168.39.84";
        // tst.Add(mm);



        IPAddress turip = IPAddress.Parse(id);
        // var rslt = _context.tenmins.Where(s => s.m_turbine_id ==turip && s.m_time_stamp >= DateTime.Now.AddDays(-1)).Take(2).ToList();

        var rslt = (from m in _context.stat10

                    where m.m_turbine_id == turip && m.m_time_stamp >= fromdate && m.m_time_stamp <= todate
                    select new tenMinsStatcs

                    {
                        m_time_stamp = m.m_time_stamp,
                        // m_turbine_id = m.m_turbine_id.ToString(),
                        m_wind_speed = m.m_wind_speed



                    }).ToList();
        return rslt;
    }

这是我的 webApiConfig:

 public static void Register(HttpConfiguration config)
    {


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

        // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
        // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
        // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
        //config.EnableQuerySupport();

        // To disable tracing in your application, please comment out or remove the following line of code
        // For more information, refer to: http://www.asp.net/web-api
        config.EnableSystemDiagnosticsTracing();
    }

知道我在哪里做错了吗? 当我运行我的代码时,在 localhost 之后我编写了 api/Values ,考虑到这一点,它应该到达控制器吗?

【问题讨论】:

  • 您能告诉我们您是如何调用该 API 方法的吗?

标签: asp.net-mvc-4 asp.net-web-api


【解决方案1】:

您需要更改 RouteConfig.cs 文件中的配置,其中定义的路由模式和 ASP.Net MVC 路由请求按照以下配置。 api 文字前缀仅在 Web API 中不适用于 MVC 操作方法,为什么您在主机名后附加 api 文字。输入 URL-HTTP sheme://domain name:port/litteral/Controller Name/Action Name 时可以执行 ASP.Net MVC 操作方法

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

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

【讨论】:

    猜你喜欢
    • 2017-03-31
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 2019-06-07
    相关资源
    最近更新 更多