【问题标题】:Controller name conflicts with folders inside project solution控制器名称与项目解决方案中的文件夹冲突
【发布时间】:2019-04-30 07:30:13
【问题描述】:

我创建了一个名为“服务”的控制器,除了默认操作不起作用之外,它工作正常。更具体地说,当我尝试联系 .../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 }
        );
    }
}

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

【问题讨论】:

  • 我用你拥有的东西创建了一个项目,我的意思是你的控制器,它工作正常。
  • 我觉得这个链接对你有帮助:stackoverflow.com/questions/44932387/…
  • 谢谢,我同时找到了原因并编辑了问题。

标签: c# asp.net-mvc routing


【解决方案1】:

您只需在RouteConfig 类中将RouteExistingFiles 设置为true

routes.RouteExistingFiles = true;

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

【讨论】:

    【解决方案2】:

    你必须更改项目中的虚拟目录

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 2014-08-08
      • 2013-12-05
      • 2013-11-06
      • 2023-03-31
      • 1970-01-01
      • 2012-12-23
      相关资源
      最近更新 更多