【问题标题】:C#, ASP MVC 4, Web API, "No type was found that matches the controller named..."C#,ASP MVC 4,Web API,“找不到与名为...的控制器匹配的类型”
【发布时间】:2017-10-12 22:36:13
【问题描述】:

这是我的问题。我继承了一个包含 3 件的项目解决方案。一个 Sharepoint 页面项目、一个 API 项目和一个 SSIS 项目。我一直在根据需要添加和学习,现在我需要指导。最近,我将 2 个标准 MVC 4 网页添加到 WEB Api 项目到 CRUD 2 个新表中。之后,我添加了一个新的 Web API 端点来查询部分新数据,以便将新的分析页面添加到 Sharepoint 项目中。我的问题是,无论我如何配置路由,我都无法让端点做出响应。我今天发现有 2 个(或更多?)引擎可以执行路由、Web API 和 ASP MVC,我相信我在项目中混合了这两种样式是我的问题......

这是我的 WebApiConfig:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Cors;
using WebApiContrib.Formatting.Jsonp;

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

        // enable CORS
        var cors = new EnableCorsAttribute("*", "*", "*");
        config.EnableCors(cors);

        // jsonp
        var jsonpFormatter = new JsonpMediaTypeFormatter(config.Formatters.JsonFormatter);
        config.Formatters.Add(jsonpFormatter);

        // Web API routes
        config.MapHttpAttributeRoutes();
        config.Routes.MapHttpRoute(
            name: "Sites",
            routeTemplate: "api/sites/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

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

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

` 还有我的 RouteConfig:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace API_MetricsWarehouse
{
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 = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}
}

还有我的 Global.asax

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace API_MetricsWarehouse
{
public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}
}

还有我最新的控制器...

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using API_MetricsWarehouse.Models;
using System.Web.Http.Cors;


namespace API_MetricsWarehouse.Controllers
{
public class SafetyDatesController : ApiController
{
    private MetricsWarehouseEntities db = new MetricsWarehouseEntities();

    // GET: api/SafetyDates
    public IQueryable<SafetyDates> GetSafetyDates()
    {            
        var result = db.SafetyAnalysis1.AsQueryable();

        result = result
            .GroupBy(x => x.YearMonth)
            .Select(x => x.FirstOrDefault())
            .Distinct()
            .OrderByDescending(x => x.YearMonth);

        return result
            .AsEnumerable()
            .Select(a =>
                new SafetyDates
                {
                    SafetyYearMonth = ((DateTime)a.YearMonth).ToString("yyyy MMM")
                })
            .AsQueryable();


    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }

}
}

新控制器几乎是 ReportDates 控制器的副本,但端点不会触发。 (ReportDates 仍然很好用......)任何人都可以帮我解开这个问题吗?我宁愿不退回到旧项目并将 MVC 页面拉出并将它们放在另一个项目中,但如果这是唯一的方法,我会这样做......

【问题讨论】:

标签: c# asp.net-mvc-4 asp.net-web-api-routing


【解决方案1】:

所以关于我的问题的更多背景知识。自 1996 年以来,我一直从事 Microsoft 产品工作。我与 Foxpro 和 VB6 进行了广泛的合作,过去几年我一直从事 VC# 工作。仍在追赶 2013 和 2015 IDE 的来龙去脉,追赶 Web 开发和 MVC 4 和 5。我最近继承的项目没有最好的文档。 (惊喜)。在为我创建的 Web 应用程序添加代码时,我遵循了以下过程:进行更改。清洁项目。重建项目。清洁解决方案。重建解决方案。在 IDE 中运行解决方案以进行测试。这适用于我构建的 2 个 mvc 网页。添加新 API 后,我更改了程序,因为我不确定如何访问端点。我试过:做出改变。清洁项目。重建项目。清洁解决方案。重建解决方案并得到意想不到的结果。然后,我打开了独立的 Chrome 并访问了当前工作的 API 端点。这很好,但是当我尝试上面创建的新端点时,我得到了上述结果。最终我尝试运行/更改现有的工作 API,但发现我看不到任何更改。最后,我部署并发布了我的新解决方案,并且更改出现了。我完全回到上面的代码,部署并发布了我的新解决方案,新端点做出了响应。我目前的问题是,在解决方案(sharepoint 解决方案和 WEB MVC 4 PAGES/API)中对 2 个项目进行更改时,在 IDE 中查看所有更改的最小流程是什么?我每次都必须部署和发布吗?如果部署始终是必要的,为什么它不是发布过程的一部分?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-21
    • 2013-07-22
    相关资源
    最近更新 更多