开发者

Class and acessibility problem c# & asp.net

开发者 https://www.devze.com 2023-03-31 18:39 出处:网络
i have small class like public static class TSM { static string TokenID = \"\"; public static string GetTSM()

i have small class like

public static class TSM
{

    static string TokenID = "";

    public static string GetTSM()
    {
        TokenID = new Guid().ToString();
        return TokenID;
    }

}

`GetTokenID `will return a string

i just call GetTokenID from my aspx page like

<script language="javascript" type="text/javascript">
    var token= <% =TSM.GetTSM() %>;

i am g开发者_StackOverflowetting error when i am running that aspx page.

the error is

Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1002: ; expected

please guide me what is going wrong. thanks


You need to bring the namespace in which this TSM class is declared into scope in your Web Page:

<%@ Import Namespace="SomeNamespace" %>
...
<script type="text/javascript">
    var token = '<%=TSM.GetTSM() %>';
</script>

or fully quote it:

<script type="text/javascript">
    var token = '<%= SomeNamespace.TSM.GetTSM() %>';
</script>

or include it in the <namespaces> section of your web.config.

Remark: Notice the '' around the <%=TSM.GetTSM() %> as there is no Guid type in javascript. You must use a string.

Also note that your server side code is not thread-safe because of the assignment of this static field. Also it will always generate an empty guid.

Here's how to improve it:

public static class TSM
{
    public static string GetTSM()
    {
        return Guid.NewGuid().ToString();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号