C#,ASP MVC 4,Web API,“找不到与名为...的控制器匹配的类型”
这是我的问题。我继承了一个有3件的项目解决方案。 Sharepoint页面项目,API项目和SSIS项目。我一直在根据需要添加和学习,现在我需要指导。最近,我在WEB Api项目中添加了2个标准MVC 4网页,以增加2个新表格。这样做之后,我添加了一个新的Web API端点来查询部分新数据,以便将新分析页面添加到Sharepoint项目中。我的问题是,无论我如何配置路由,我都无法让端点响应。我今天发现有2个(或更多)引擎可以执行路由,Web API和ASP MVC,我相信我在项目中混合使用了2种风格是我的问题...C#,ASP MVC 4,Web API,“找不到与名为...的控制器匹配的类型”
这里是我的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页面,并把他们在另一个项目,但我会如果这是唯一的出路...
因此,多一点背景对我的问题。自1996年以来,我一直在从事微软产品的工作。我与Foxpro和VB6进行了广泛的合作,过去几年我一直在使用VC#进行工作。还在追赶2013年和2015年IDE的来龙去脉,赶上Web开发和MVC 4和5.我最近继承的项目没有最好的文档。 (惊喜)。在为我创建的Web应用程序添加代码时,遵循了以下过程:进行更改。清洁项目。重建项目。清洁方案。重建解决方案。在IDE中运行解决方案以进行测试。这对我建立的2个mvc网页很好。一旦我添加了新的API,我改变了我的过程,因为我不确定如何访问终点。我尝试过:做出改变。清洁项目。重建项目。清洁方案。重建解决方案并获得意想不到的结果。然后,我单独打开Chrome并访问当前正在运行的API端点。这工作得很好,但是当我尝试了上面创建的新端点时,我得到了上述结果。最终我试图运行/更改现有的工作API,但发现我看不到任何更改。最后,我部署并发布了我的新解决方案,并且发生了变化。我精确地回到了上面的代码,部署和发布了我的新解决方案,并且NEW端点做出了回应。我当前的问题是在解决方案中的2个项目(共享点解决方案和WEB MVC 4 PAGES/API)中进行更改时,查看IDE内所有更改的最小流程是什么?我是否必须每次部署和发布?如果始终需要部署,为什么它不是发布过程的一部分?
你在尝试什么样的网址? – Shyju
https://internalSite.com/_metrics/api/safetydates:破解https://internalSite.com/_metrics/api/reportdates:作品 – MultiWolf