我有一个显示活动详情的详细信息页面。
本页底部有一个注册按钮。
@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();
}
点击“注册”按钮后,这是链接
这是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个路由:
public static void RegisterRoutes(RouteCollection路由) { routes.IgnoreRoute(&#34; {资源}个.axd / {*} PATHINFO&#34);
//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 }
);
}
然后我终于得到了一些不同的东西,但它仍然是一个错误:
&#39; / lrc&#39;中的服务器错误应用
目前的类型, Microsoft.AspNet.Identity.IUserStore`1 [lrc.Models.ApplicationUser],是 一个接口,无法构造。你错过了一个类型吗? 映射?
描述:执行期间发生了未处理的异常 当前的网络请求。请查看堆栈跟踪了解更多信息 有关错误的信息以及它在代码中的起源。
异常详细信息:System.InvalidOperationException:当前类型, Microsoft.AspNet.Identity.IUserStore`1 [lrc.Models.ApplicationUser],是 一个接口,无法构造。你错过了一个类型吗? 映射?
来源错误:
执行期间生成了未处理的异常 当前的网络请求。有关的来源和位置的信息 可以使用下面的异常堆栈跟踪来识别异常。
堆栈追踪:
[InvalidOperationException:当前类型, Microsoft.AspNet.Identity.IUserStore
1[lrc.Models.ApplicationUser], is an interface and cannot be constructed. Are you missing a type mapping?]
1 resolverOverrides)+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:依赖项的解析失败,输入 =&#34; lrc.Controllers.AccountController&#34;,name =&#34;(none)&#34;。在解决时发生异常:例外情况是: InvalidOperationException - 当前类型, Microsoft.AspNet.Identity.IUserStore`1 [lrc.Models.ApplicationUser],是 一个接口,无法构造。你错过了一个类型吗? 映射? - - - - - - - - - - - - - - - - - - - - - - - - 当时例外,容器是:
解析lrc.Controllers.AccountController,(无)解析 参数&#34; userManager&#34;构造函数 lrc.Controllers.AccountController(lrc.ApplicationUserManager userManager,lrc.ApplicationSignInManager signInManager) 解析lrc.ApplicationUserManager,(无) 解析参数&#34; store&#34;构造函数lrc.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],(none) ] Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t,Object 现有的,字符串名称,IEnumerable1 resolverOverrides) +550
1 resolverOverrides)+20
Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name, IEnumerable
System.Web.Mvc.DefaultControllerActivator.Create(的RequestContext requestContext,Type controllerType)+78[InvalidOperationException:尝试创建时出错 控制器类型&#39; lrc.Controllers.AccountController&#39;。确保这一点 控制器有一个无参数的公共构造函数。]
System.Web.Mvc.DefaultControllerActivator.Create(的RequestContext requestContext,Type controllerType)+256
System.Web.Mvc.DefaultControllerFactory.CreateController(的RequestContext requestContext,String controllerName)+169
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext,IController&amp;控制器,IControllerFactory&amp;厂) +270 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,AsyncCallback回调,对象状态)+147
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +12289179 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; 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内):
//配置此应用程序中使用的应用程序用户管理器。 UserManager在ASP.NET Identity中定义,并由应用程序使用。 公共类ApplicationUserManager:UserManager { public ApplicationUserManager(IUserStore store) :基地(商店) { }
答案 0 :(得分:1)
发布工作正常。正如@Stephen在评论中指出的那样,使用动作参数发布应该可以正常工作。
作为替代方案,如果由于某种原因它不起作用,您可以使用隐藏字段作为事件ID:
Html.Hidden("eventId", Model.Id)
这样就可以很好地发布了。
答案 1 :(得分:0)
我怀疑无法找到与您的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 }
);