点击表单中的按钮产生“;找不到资源“;
本文关键字:找不到 资源 按钮 表单 | 更新日期: 2023-09-27 17:59:08
我有一个显示事件详细信息的Details页面。
此页面底部有一个"注册"按钮。
@using (Html.BeginForm("Register", "Events", new { eventId = Model.Id }, FormMethod.Post, null))
{
@Html.AntiForgeryToken()
<div id="">
<p>
@*<button type="submit" class="btn btn-default" id="btnEnrol" name="btnEnrol" value="@Model.Id" title="Register">Register</button>*@
<input type="submit" class="btn btn-default" id="btnEnrol" name="btnEnrol" value="Register" />
</p>
</div>
}
这是我的控制器方法:
[HttpPost]
[Authorize]
[ValidateAntiForgeryToken]
public ActionResult Register(int eventId)
{
if (eventId == 0)
return View("Index");
var now = DateTime.Now;
var userId = User.Identity.GetUserId();
var registration = this.repo.GetRegistration(eventId, userId);
if (registration != null)
{
ViewBag.Registered = true;
}
else
{
var successful = this.repo.RegisterEvent(eventId, userId);
if (successful)
{
return View();
}
else
{
return View();
}
}
return View();
}
这是点击注册按钮后的链接
http://localhost/lrc/Events/Register?eventId=7
这是RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapRoute(
// name: "Home",
// url: "Home/{action}/{id}",
// defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
//);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
我读了很多关于SO的帖子,但没有一个能解决我的问题。
我也尝试过:
删除了[HttpPost]注释,我认为这没有帮助,但这只是一次绝望的尝试。
更新1
在默认路由之前,我在RouteConfig.cs中又添加了1条路由:
公共静态void RegisterRoutes(RouteCollection路由){路线。IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapRoute(
// name: "Home",
// url: "Home/{action}/{id}",
// defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
//);url: "Event/Register/{eventId}"
routes.MapRoute(
name: "myRoute",
url: "Events/Register/{eventId}",
defaults: new { controller = "Events", action = "Register", eventId = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
然后我终于得到了一些不同的东西,但它仍然是一个错误:
"/lrc"应用程序中的服务器错误。
当前类型,微软AspNet。身份IUserStore `1[lrc.Models.ApplicationUser],是接口,并且无法构造。你缺少一个类型吗映射?
描述:在执行期间发生未处理的异常当前web请求。请查看堆栈跟踪以了解更多信息有关错误的信息以及错误在代码中的来源。
异常详细信息:系统。InvalidOperationException:当前类型,微软AspNet。身份IUserStore `1[lrc.Models.ApplicationUser],是接口,并且无法构造。你缺少一个类型吗映射?
来源错误:
在执行期间生成了未处理的异常当前web请求。关于可以使用下面的异常堆栈跟踪来识别异常。
堆栈跟踪:
InvalidOperationException:当前类型,微软AspNet。身份IUserStore
1[lrc.Models.ApplicationUser], is an interface and cannot be constructed. Are you missing a type mapping?]
1解析器覆盖)+442
Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForAttemptingToConstructInterface(IBuilderContext context) +192 lambda_method(Closure , IBuilderContext ) +40
Microsoft.Practices.ObjectBuilder2.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context) +48
Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +323
Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +349
Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey) +254 lambda_method(Closure , IBuilderContext ) +200
Microsoft.Practices.ObjectBuilder2.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context) +48
Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +323
Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +349
Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey) +254 lambda_method(Closure , IBuilderContext ) +202
Microsoft.Practices.ObjectBuilder2.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context) +48
Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +323
Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +349
Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable[ResolutionFailedException:依赖项的解析失败,类型为="lrc.Controllers.AccountController",name="(none)"。解析时发生异常。例外情况是:InvalidOperationException-当前类型,微软AspNet。身份IUserStore `1[lrc.Models.ApplicationUser],是接口,并且无法构造。你缺少一个类型吗映射?-----------------------------------------------发生异常时,容器为:
正在解析lrc。控制器。AccountController,(无)正在解析构造函数的参数"userManager"lrc。控制器。AccountController(lrc.ApplicationUserManageruserManager,lrc。应用程序登录管理器登录管理器)正在解析lrc。ApplicationUserManager,(无)正在解析构造函数lrc的参数"store"。ApplicationUserManager(Microsoft.AspNet.Identity.IUserStore
1[[lrc.Models.ApplicationUser, lrc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] store) Resolving Microsoft.AspNet.Identity.IUserStore
1[lrc.Models.ApplicationUser],(无)]微软。实践。团结一致UnityContainer。DoBuildUp(类型t,对象existing,String name,IEnumerable1 resolverOverrides) +550
1 resolverOverrides)+20
Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name, IEnumerable
系统网状物Mvc。DefaultControllerActivator。创建(RequestContextrequestContext,Type controllerType)+78InvalidOperationException:尝试创建lrc类型的控制器。控制器。AccountController。确保控制器有一个无参数的公共构造函数。]
系统网状物Mvc。DefaultControllerActivator。创建(RequestContextrequestContext,Type controllerType)+256
系统网状物Mvc。默认控制器工厂。CreateController(RequestContextrequestContext,字符串控制器名称)+169
系统网状物Mvc。MvcHandler。ProcessRequestInit(HttpContextBasehttpContext,IController&控制器,IControllerFactory&工厂)+270系统。网状物Mvc。MvcHandler。BeginProcessRequest(HttpContextBasehttpContext,AsyncCallback回调,对象状态)+147
系统网状物CallHandlerExecutionStep。系统网状物HttpApplication。I执行步骤。执行()+12289179系统。网状物HttpApplication。ExecuteStep(IExecutionStep步骤,Boolean和completedSynchronously)+288版本信息:Microsoft。NET Framework版本:4.0.30319;ASP。NET版本:4.0.30319.34248
更新2
这是我的EventsController构造函数
private ILRCRepository repo;
public EventsController(ILRCRepository repository)
{
this.repo = repository;
}
我在整个项目中搜索了UserStore,只在一个地方找到了它(在IdentityConfig.cs中):
// Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
Post可以正常工作。正如@Stephen在评论中指出的那样,带有动作参数的帖子应该可以正常工作。
作为一种选择,如果由于某种原因它不起作用,您可以使用事件ID的隐藏字段:
Html.Hidden("eventId", Model.Id)
这会很好地发布到行动中。
我怀疑找不到资源与您的RouteConfig.cs需要{id}和EventsController
需要{eventId}有关。
更改EventsController方法,使int id
作为输入参数:
[HttpPost]
[Authorize]
[ValidateAntiForgeryToken]
public ActionResult Register(int id)
{
if (id == 0)
return View("Index");
var now = DateTime.Now;
var userId = User.Identity.GetUserId();
var registration = this.repo.GetRegistration(id, userId);
if (registration != null)
{
ViewBag.Registered = true;
}
else
{
var successful = this.repo.RegisterEvent(id, userId);
if (successful)
{
return View();
}
else
{
return View();
}
}
return View();
}
或者,您可以更新RouteConfig.cs,以包含一条具有{eventId}的路由:
routes.MapRoute(
name: "Event",
url: "{Irc}/Events/Register/{eventId}",
defaults: new { controller = "Events", action = "Register", eventId = UrlParameter.Optional }
);