你好,我目前正在開發一個ASP.NET MVC 3應用程序,使用新的剃刀視圖engien。 但我有一些問題,該劑量不會產生當我測試在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中,即使使用相同的數據,也不會產生此錯誤。我甚至嘗試在Model.Album.UserId前加上(int) 我不明白爲什麼我有這個錯誤,或者如何解決它。
調用堆棧:
[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;
}
是的,我一直在想這個,但我無法找到問題,我已添加功能的第一篇文章 – Androme 2011-04-22 03:07:41
嗯,有趣。它確實出現在'GetUserId()'中,但'int.TryParse'應該特別避免這種確切的異常。是否有可能'int.TryParse'最近被從'int.Parse'改變了,並且在活着的服務器上有一箇舊版本的代碼?這是我能想到的唯一的事情。順便說一句,你的系統是否有'User.Identity.Name'是一個整數?這對我來說似乎不尋常。只是好奇。 – ataddeini 2011-04-22 03:21:56
是的核心是int.Parse之前,但我將其更改爲int.TryParse,但問題仍然存在,我只是使用User.Identity.Name來保存我的userId而不是用戶名 – Androme 2011-04-22 03:28:35