控制器名称与项目解决方案中的文件夹冲突

时间:2019-04-30 07:30:13

标签: c# asp.net-mvc routing

我创建了一个名为“服务”的控制器,该控制器可以正常工作,但默认操作无效。更具体地说,当我尝试到达.../Services时,我得到

  

HTTP错误403.14-禁止

错误,但.../Services/Index有效。所有其他控制器运行正常。我做错了什么,还是有保留的路线或类似的东西?我已经删除了RouteConfig中的“ IgnoreRoute”,但仍然无法正常工作。

这是控制器代码:

public class ServicesController : Controller
{
    private ApplicationDbContext db = new ApplicationDbContext();

    // GET: Services
    public ActionResult Index(int CategoryId = -1)
    {
        if (CategoryId == -1)
        {
            var services = db.Services.Include(s => s.ServiceCategory).Include( i => i.Images);
            return View(services.ToList());
        }
        else
        {
            var services = db.Services.Where(r => r.ServiceCategoryId == CategoryId).Include(p => p.ServiceCategory).Include(i => i.Images);
            return View(services.ToList());
        }
    }
}

以及路由配置:

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

编辑: 我找到了原因,在项目的解决方案中有一个名为“服务”的文件夹。但是我认为这种行为是不可接受的,所以我的问题是对此的解决方案是什么?

2 个答案:

答案 0 :(得分:0)

RouteExistingFiles类中将true设置为RouteConfig所需的全部内容。

routes.RouteExistingFiles = true;

最简单的方法可能是将文件夹重命名为其他名称,以避免发生这种冲突。

答案 1 :(得分:0)

您必须在项目中更改虚拟目录

像这样 https://i.stack.imgur.com/1IQXV.png