這是我的問題。我繼承了一個有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頁面,並把他們在另一個項目,但我會如果這是唯一的出路...
你在嘗試什麼樣的網址? – Shyju
https://internalSite.com/_metrics/api/safetydates:破解https://internalSite.com/_metrics/api/reportdates:作品 – MultiWolf