gpt4 book ai didi

asp.net-mvc - MVC 5 Web 属性路由不起作用

转载 作者:行者123 更新时间:2023-12-02 13:31:54 28 4
gpt4 key购买 nike

我在我的应用程序中实现了属性路由,之后当我开始时,一切都没有按计划进行。只有 Json 结果工作其余部分未按预期工作。

 [RoutePrefix("ProductCategory")]
public class CategoryController : Controller
{
[Route("CategoryMain")]
// GET: /Category/
public ActionResult Index()
{
var cat = categoryService.GetCategories();

if (Request.IsAjaxRequest())
{
return PartialView("Index", cat);
}
return View("Index", cat);
}
}

错误

Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

请求的 URL:/ProductCategory/MainIndex我也尝试过使用 Just Index,即使现在不起作用

但是如果该方法是 JsonResult 它将返回 json 格式的数据。它不适用于任何其他 ActionResults 我的RouteConfig

 public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Category", action = "Index", id = UrlParameter.Optional }
);


}

我的 webapiConfig

public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();

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

我的全局

 AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

// Autofac and Automapper configurations
Bootstrapper.Run();

最佳答案

给定路由前缀和下面的路由

[RoutePrefix("ProductCategory")]
public class CategoryController : Controller {
[HttpGet]
[Route("CategoryMain")] // Matches GET ProductCategory/CategoryMain
public ActionResult Index() {
var cat = categoryService.GetCategories();
if (Request.IsAjaxRequest()) {
return PartialView("Index", cat);
}
return View("Index", cat);
}
}

请求的URL需要是

ProductCategory/CategoryMain

对于CategoryController.Index操作

关于asp.net-mvc - MVC 5 Web 属性路由不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45120965/

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