使用名为“PropertiesController”的控制器的ASP.NET MVC路由

时间:2022-06-26 17:12:08

I'm having a tricky issue (bear with me as I'm new to MVC) with trying to use a controller (and a route subsequently) with the name PropertiesController.

尝试使用名为PropertiesController的控制器(以及随后的路由)时,我遇到了一个棘手的问题(因为我是MVC的新手)。

I believe this is because there is a directory (which I can't really remove) called "Properties" in my solution. Is there a way round this?

我相信这是因为在我的解决方案中有一个名为“Properties”的目录(我无法删除)。这有什么办法吗?

The route setup is just one simple route:

路线设置只是一条简单的路线:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Properties", action = "List", id = "" } // Parameter defaults 
);

and the error I get in IIS7 when requesting "http://localhost/aptment2/properties/" is:

并且在请求“http:// localhost / aptment2 / properties /”时我在IIS7中得到的错误是:

使用名为“PropertiesController”的控制器的ASP.NET MVC路由

Surely there is a way round this that I just can't find? Cheers.

当然有一种方法可以找到我找不到的方法吗?干杯。

4 个解决方案

#1


5  

Since someone else is having the same problem, I'm guessing that you're running into a reserved keyword here (I imagine ClassControllers are out as well).

由于其他人遇到了同样的问题,我猜你在这里遇到了一个保留关键字(我想象ClassControllers也是如此)。

It's not the first time it's happened either - digging deeper into the internals of the environment, you can't route to Windows' reserved keywords either.

这不是第一次发生 - 深入挖掘环境的内部,你也无法路由到Windows的保留关键字。

#2


4  

I have a PropertiesController setup on a MVC 3 application configured to use IIS Express. I don't get the exact error that you do, but it is similar (404.20 no default document) and I'm sure it is for the same reason.

我在配置为使用IIS Express的MVC 3应用程序上设置了PropertiesController。我没有得到你所做的确切错误,但它是相似的(404.20没有默认文件),我确信它是出于同样的原因。

It doesn't cause a problem at all in our production environment since we publish a compiled build of the app and then it doesn't have a Properties folder.

它在我们的生产环境中根本不会引起问题,因为我们发布了应用程序的编译版本,然后它没有Properties文件夹。

To get around the issue while developing locally I've got all my static content (scripts/css/etc...) in a single directory and I ignore it while also turning on RouteExistingFiles.

为了在本地开发时解决这个问题,我将所有静态内容(scripts / css / etc ...)放在一个目录中,并且在打开RouteExistingFiles的同时忽略它。

  routes.IgnoreRoute("static/{*wildcard}");
  routes.RouteExistingFiles = true;

This fixed my issue and now I can browse example.com/properties/ error free.

这解决了我的问题,现在我可以免费浏览example.com/properties/。

#3


2  

Running a quick test, I get the same behavior. Is it a viable option to use something other than Properties?

运行快速测试,我得到相同的行为。使用除Properties之外的其他东西是否可行?

#4


0  

I don't know if it's an option for you, but you could try to disable routing to existing files.

我不知道它是否适合您,但您可以尝试禁用路由到现有文件。

routes.RouteExistingFiles = false;

#1


5  

Since someone else is having the same problem, I'm guessing that you're running into a reserved keyword here (I imagine ClassControllers are out as well).

由于其他人遇到了同样的问题,我猜你在这里遇到了一个保留关键字(我想象ClassControllers也是如此)。

It's not the first time it's happened either - digging deeper into the internals of the environment, you can't route to Windows' reserved keywords either.

这不是第一次发生 - 深入挖掘环境的内部,你也无法路由到Windows的保留关键字。

#2


4  

I have a PropertiesController setup on a MVC 3 application configured to use IIS Express. I don't get the exact error that you do, but it is similar (404.20 no default document) and I'm sure it is for the same reason.

我在配置为使用IIS Express的MVC 3应用程序上设置了PropertiesController。我没有得到你所做的确切错误,但它是相似的(404.20没有默认文件),我确信它是出于同样的原因。

It doesn't cause a problem at all in our production environment since we publish a compiled build of the app and then it doesn't have a Properties folder.

它在我们的生产环境中根本不会引起问题,因为我们发布了应用程序的编译版本,然后它没有Properties文件夹。

To get around the issue while developing locally I've got all my static content (scripts/css/etc...) in a single directory and I ignore it while also turning on RouteExistingFiles.

为了在本地开发时解决这个问题,我将所有静态内容(scripts / css / etc ...)放在一个目录中,并且在打开RouteExistingFiles的同时忽略它。

  routes.IgnoreRoute("static/{*wildcard}");
  routes.RouteExistingFiles = true;

This fixed my issue and now I can browse example.com/properties/ error free.

这解决了我的问题,现在我可以免费浏览example.com/properties/。

#3


2  

Running a quick test, I get the same behavior. Is it a viable option to use something other than Properties?

运行快速测试,我得到相同的行为。使用除Properties之外的其他东西是否可行?

#4


0  

I don't know if it's an option for you, but you could try to disable routing to existing files.

我不知道它是否适合您,但您可以尝试禁用路由到现有文件。

routes.RouteExistingFiles = false;