gpt4 book ai didi

c# - 如何在命名空间中路由混合的 MvcApplication 和 WebApiApplication?

转载 作者:行者123 更新时间:2023-11-30 23:28:00 24 4
gpt4 key购买 nike

我有一个带有两个 Controller 的 MVC 应用程序,一个用作 WebAPIApplication,第二个用作常规 MvCApplication。我已经以某种方式配置了只能访问 API 的路由。

你能帮我配置一个我可以访问两个 Controller 的方式吗?

我可以通过指向 http://localhost/api/data 来访问 API 的 DefaultController。但是,我无法访问 MvcApplication Index 方法。我的期望是通过指向 http://localhost/Notification 来访问它,但我收到错误 404!

这是我的 Controller C# 源代码:

//DefaultController.cs    
namespace MyApp.Controllers
{
[RoutePrefix("api")]
public class DefaultController : ApiController
{
[Route("data")]
[AcceptVerbs("GET")]
public HttpResponseMessage GetMeData()
{
XYZ...
}
}
}

//NotificationController.cs
namespace MyApp.Controllers
{
public class NotificationController : Controller
{
public ActionResult Index()
{
return this.Jsonp(Rep.All());
}
}
}

下面是我如何设置到这两个 Controller 的路由:

//Global.asax.cs
namespace MyApp
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}

public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Notification", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Notification", action = "Index" } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
}

//WebApiConfig.cs
namespace MyApp
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}

最佳答案

正如@ChimaOsuji 在评论中提到的,

You've created two applications, WebApiApplication and MvcApplication. You only need one, in which you register routes for both MVC and API controllers.

Global.asax.cs

namespace MyApp
{

public class WebApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Notification", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Notification", action = "Index" } // Parameter defaults
);
}
protected void Application_Start()
{
//Web api config
GlobalConfiguration.Configure(WebApiConfig.Register);
//MVC config
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
}

关于c# - 如何在命名空间中路由混合的 MvcApplication 和 WebApiApplication?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36202956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com