2014-01-19 55 views
0

我想在我的C#頁面執行以下代碼。我知道這裏有更多這樣的問題,但我找不到可以幫助我的東西。不過,我在第一行在aspx處包含C#文件代碼

The server block is not well formed 

得到一個服務器錯誤代碼是:

<%@using Newtonsoft.Json.Linq; %> 
<%@using System; %> 
<%@using Project.Models;%> 
<%@using Project.Controllers;%> 

WebIntegrationRestService<int,int> service= new WebIntegrationRestService<int,int>(); 
service.GetUserByUsername(0,1,User.Identity.Name); 
UserType type = null; 
if (User.Identity.IsAuthenticated) 
{ 
    UserType type = service.GetUserByUsername(0, 1, User.Identity.Name).First().UserType; 
} 

if (type==UserType.TypeA) 
{ 
    %> <li><%: Html.ActionLink("Add User ", "Create", "User")%></li> <% 
} 
+0

你爲什麼在一個文件中使用「導入」和「使用」?導入是VB.NET - 使用的是C#等價物。 – sh1rts

+0

是的,我把它們抄錯了。它正在使用我想要的,現在我正在解決這個問題。然而,這不是問題.. –

+0

'@ import'是一個頁面指令,它可以在代碼文件中執行'using'操作,而且不管頁面語言如何。 '@使用'是無用的,這是行不通的。 –

回答

0

你有不平衡的代碼標記。

<%@ import Newtonsoft.Json.Linq; %> 
<%@ import System; %> 
<%@using Project.Models;%> 
<%@using Project.Controllers;%> 

<% 

    WebIntegrationRestService<int,int> service= new WebIntegrationRestService<int,int>(); 
    service.GetUserByUsername(0,1,User.Identity.Name); 
    UserType type = null; 
    if (User.Identity.IsAuthenticated) 
    { 
     UserType type = service.GetUserByUsername(0, 1, User.Identity.Name).First().UserType; 
    } 


    if (type==UserType.TypeA){%> 

    <li><%: Html.ActionLink("Add User ", "Create", "User")%></li>   

    <% } %> 

注意我在頁面級聲明和代碼開始之間添加了開頭代碼標記。

我還在最後一個大括號後加了一個。

你應該重新考慮你在做什麼。當你開始在視圖中放置這種東西時,你會遇到麻煩(維護視圖中的維護成本,調試問題,重複代碼等)。

+0

非常感謝,但這並沒有幫助我。我得到完全相同的錯誤,完全相同。基本上,我試圖做的是檢查登錄用戶的類型,以顯示不同類型的用戶不同的菜單。你知道更清楚的方法嗎? –