MVC 5 Web属性路由不起作用 [英] MVC 5 Web Attribute Routing is not working

查看:147
本文介绍了MVC 5 Web属性路由不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I implemented the Attribute routing in my Application and after that when I started nothing was working as planned. Only the Json Results works rest is not working as expected.

 [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);
        }
    }

错误


/应用程序中的服务器错误。无法找到该资源。
描述:HTTP404。您正在查找的资源(或其
依赖项之一)可能已被删除,名称更改或
暂时不可用。请检查以下URL,并确保
拼写正确。

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,即使它现在不能正常工作

Requested URL: /ProductCategory/MainIndex I've also tried with Just Index even that is not working now

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

But if The method is JsonResult it will return me the data in json format. Its not working on any other ActionResults My 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();


推荐答案

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

Given the route prefix and route below

[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 操作

这篇关于MVC 5 Web属性路由不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆