我正在尝试导航到一个页面,其URL的格式如下:localhost:xxxxx / User / {id} / VerifyEmail?secretKey = xxxxxxxxxxxxxxx
我在RouteConfig.cs
文件中添加了一条新路线,所以我的RouteConfig.cs
看起来像这样:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "VerifyEmail",
url: "User/{id}/VerifyEmail",
defaults: new { controller = "User", action = "VerifyEmail" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);
}
}
不幸的是,当我尝试导航到该URL时,我得到了这个页面:
<Error>
<Message>
No HTTP resource was found that matches the request URI 'http://localhost:52684/User/f2acc4d0-2e03-4d72-99b6-9b9b85bd661a/VerifyEmail?secretKey=e9bf3924-681c-4afc-a8b0-3fd58eba93fe'.
</Message>
<MessageDetail>
No type was found that matches the controller named 'User'.
</MessageDetail>
</Error>
这是我的UserController:
public class UserController : Controller
{
// GET /User/{id}/VerifyEmail
[HttpGet]
public ActionResult VerifyEmail(string id, string secretKey)
{
try
{
User user = UsersBL.Instance.Verify(id, secretKey);
//logger.Debug(String.Format("User %s just signed-in in by email.",
user.DebugDescription()));
}
catch (Exception e)
{
throw new Exception("Failed", e);
}
return View();
}
}
请告诉我,我做错了什么?
在我的情况下花了差不多30分钟试图解决问题后,我发现了导致它的原因:
我在WebApiConfig.cs
中定义的路线是这样的:
config.Routes.MapHttpRoute(
name: "ControllersApi",
routeTemplate: "{controller}/{action}"
);
它应该是这样的:
config.Routes.MapHttpRoute(
name: "ControllersApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
如你所见,它干扰了RouteConfig.cs
中定义的标准路线。
在我的解决方案中,我有一个名为“P420”的项目,在另一个项目中,我有一个P420I控制器。
当.NET剪切控制器名称来查找路由时,与其他项目冲突,用作库。
希望能帮助到你。
在我的解决方案中,当我将新的Controller添加到项目中时,向导询问我是否要将控制器的位置设置到App_Code文件夹中。向导警告我,如果我没有将它找到App_Code文件夹,将找不到控制器类型。但我没有阅读整个警告,因为我想将文件定位到其他地方..所以这就是为什么它对我不起作用。
我添加了一个新的控制器并让它默认在App_Code中,一切正常。
面临同样的问题。检查了所有的答案,但我的问题是在命名空间。路由属性存在于System.Web.Mvc和System.Web.Http中。我的使用包括Mvc命名空间,这就是原因。对于webapi,您需要使用System.Net.Http。
在我的情况下,我称之为APi
http://locahost:56159/api/loginDataController/GetLoginData
虽然应该是这样的
http://locahost:56159/api/loginData/GetLoginData
从URL删除控制器,它开始工作......
和平!
在我的例子中,这是WebHostHttpControllerTypeResolver过度激进的缓存。
固定:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root
信用:https://sitecore.stackexchange.com/questions/9897/webapi-controllers-not-being-found-in-sitecore-8-2
在我的例子中,控制器被定义为:
public class DocumentAPI : ApiController
{
}
将其更改为以下工作!
public class DocumentAPIController : ApiController
{
}
类名必须以Controller结束!
编辑:正如@Corey Alix建议的那样,请确保控制器具有公共访问修饰符;路由处理程序忽略非公共控制器!
另一种解决方案是将控制器类权限设置为public。
设置这个:
class DocumentAPIController : ApiController
{
}
至:
public class DocumentAPIController : ApiController
{
}
在我的情况下,我使用Web API,我没有为我的控制器类定义public
。
要检查Web API的事情:
public
: ApiController
Controller
结尾/api/
前缀。例如。 '主机:端口/ API / {控制器} / {actionMethod}'在我的情况下,我想创建一个Web API控制器,但由于疏忽,我的控制器继承自Controller而不是ApiController。
在我的例子中,路由被定义为:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{*catchall}",
defaults: new { controller = "WarehouseController" }
而Controller需要在配置中删除:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{*catchall}",
defaults: new { controller = "Warehouse" }
在我的情况下,我看到这是因为我有两个同名的控制器:
一个用于处理客户订单,称为CustomersController
,另一个用于获取事件,也称为CustomersController
我错过了重复,我将事件重命名为CustomerEventsController
,它完美无缺
经历过类似的问题。我们正在处理多个API,我们遇到错误的端口号并收到此错误。让我们永远意识到。确保您正在击中的api的端口是正确的端口。
我也面临同样的问题。我搜索了很多,发现需要类级别权限。默认情况下,类权限级别是内部的,所以我认为它不会影响程序执行。但它实际上受到影响,你应该公开你的班级许可,这样你就不会遇到任何问题。
还有一个。如果是webapi项目,您的webapirouteconfig文件将覆盖routeconfig.cs文件设置。因此,更新webapi routeconfig文件也可以正常工作。