我将以下代码放入我的家庭 Controller 中。但是当我尝试导航到 localhost:xxx/chkDoc 时,出现 404 错误。但是当我将相同的代码放在其他 Controller 中时,我成功并返回了适当的字符串。我的所有设置都是VS2012生成的默认项目设置。知道为什么这种情况发生/仅在其他 Controller 中起作用吗?
public string chkDoc()
{
if (User.Identity.IsAuthenticated)
{
return "authorized";
}
else
{
return "gotologin";
}
}
最佳答案
url(路由)必须包含 Controller 名称和操作方法名称 - 按此顺序。您可以跳过操作名称(将替换默认值),也可以同时跳过 Controller 和操作名称(两者均使用默认值),但不能跳过 Controller 名称。
关于c# - 为什么字符串方法在家庭 Controller 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17493714/