Здравствуйте. В настоящее время я занимаюсь разработкой приложения ASP.NET MVC 3, в котором используется новый вид бритвы.
Но у меня возникла проблема, когда при тестировании домашней страницы в Visual Studio 2010 доза не накапливается.
Пример одной из моих страниц имеет следующий код
Line 37: <div class="info">
Line 38: <a href="@LinkHelper.DisplayProfileUrl((int)Model.Album.User.Id, Model.Album.User.Username)">@Model.Album.User.Username</a>
Line 39: @if (UserHelper.IsModeratorOrAdministrator(Model.Album.UserId)) { <text>(<a href="@LinkHelper.AlbumEditUrl(Model.Album)">Manage Album</a>)</text>}
Line 40: </div>
Введите следующую ошибку в строке 39
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
Теперь в Visual Studio 2010 доза этой ошибки не накапливается даже при тех же данных. я даже пытался поставить (int) перед Model.Album.UserId
Я не понимаю, почему у меня возникает эта ошибка или как ее исправить.
Стек вызовов:
[FormatException: Input string was not in a correct format.]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +12630485
System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +224
Cosplay.Helpers.UserHelper.GetUserId() in C:\CosplayTFS\Website\Branches\Albums\Cosplay\Helpers\UserHelper.cs:50
Cosplay.Helpers.UserHelper.IsModeratorOrAdministrator(Int32 userId) in C:\CosplayTFS\Website\Branches\Albums\Cosplay\Helpers\UserHelper.cs:36
ASP.<>c__DisplayClass5.<Execute>b__4() in c:\Server\Web\Kasper\test.cosplay.dk\Views\Albums\AlbumDetails.cshtml:39
System.Web.WebPages.<>c__DisplayClassb.<RenderSection>b__9(TextWriter tw) +289
System.Web.WebPages.WebPageBase.Write(HelperResult result) +89
ASP._Page_Views_Shared__Layout_cshtml.Execute() in c:\Server\Web\Kasper\test.cosplay.dk\Views\Shared\_Layout.cshtml:61
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +104
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +173
System.Web.WebPages.WebPageBase.Write(HelperResult result) +89
System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) +234
System.Web.WebPages.WebPageBase.PopContext() +234
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +384
System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +33
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +784900
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +265
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +784976
System.Web.Mvc.Controller.ExecuteCore() +159
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +54
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371
Функции
public static bool IsAuthenticated()
{
return HttpContext.Current.User.Identity.IsAuthenticated;
}
public static bool IsModeratorOrAdministrator(int userId)
{
if (!IsAuthenticated())
return false;
return GetUserId() == userId || (Roles.IsUserInRole("Moderator") || Roles.IsUserInRole("Administrator"));
}
public static int GetUserId()
{
int tryp;
if(!int.TryParse(HttpContext.Current.User.Identity.Name, out tryp))
return -1;
return tryp;
}