将字符串值转换回 GUID 值

发布于 2024-09-01 14:27:57 字数 137 浏览 5 评论 0原文

我有一个 guid 值,存储在隐藏变量中。 例如 (303427ca-2a5c-df11-a391-005056b73dd7)

现在我如何将这个隐藏字段的值转换回 GUID 值(因为我要调用的方法需要 GUID 值)。

谢谢。

i have a guid value that i store in my hidden variable.
say for eg (303427ca-2a5c-df11-a391-005056b73dd7)

now how do i convert the value from this hidden field back to GUID value (because the method i would be calling expects a GUID value).

thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

忘东忘西忘不掉你 2024-09-08 14:27:57

只需使用重载的构造函数:

try
{
  Guid guid = new Guid("{D843D80B-F77D-4655-8A3E-684CC35B26CB}");
}
catch (Exception ex) // There might be a more appropriate exception to catch
{
  // Do something here in case the parsing fails.
}

Just use the overloaded constructor:

try
{
  Guid guid = new Guid("{D843D80B-F77D-4655-8A3E-684CC35B26CB}");
}
catch (Exception ex) // There might be a more appropriate exception to catch
{
  // Do something here in case the parsing fails.
}
破晓 2024-09-08 14:27:57

通过将 Guid 存储在字符串中,可以让攻击者变得非常容易。比如说,在分页文件中查找起来很简单。将其存放在Guid中,并用一石杀死两只鸟。

You are making it pretty easy on an attacker by storing the Guid in a string. Trivial to find back in, say, the paging file. Store it in a Guid and kill two birds with one stone.

饭团 2024-09-08 14:27:57
    string strGuid;
    strGuid = (your guid here);
    Guid guid = new Guid(strGuid);

有关详细信息,MSDN

    string strGuid;
    strGuid = (your guid here);
    Guid guid = new Guid(strGuid);

For more info, MSDN

沩ん囻菔务 2024-09-08 14:27:57

Guid 有一个字符串 Guid 的构造函数。

Guid guid = new Guid(myStringGuid);

Guid has a constructor for string Guids.

Guid guid = new Guid(myStringGuid);
颜漓半夏 2024-09-08 14:27:57

新的 Guid(myHiddenFieldString)

new Guid(myHiddenFieldString)

夏末的微笑 2024-09-08 14:27:57

我认为可以简单地按如下方式完成:

Guid MyGuid = new Guid(stringValue);

I think it can be done simply as following:

Guid MyGuid = new Guid(stringValue);
神经大条 2024-09-08 14:27:57

在 .NET4 及以上版本中,您还可以使用:

Guid myGuid = Guid.Parse(myGuidString); 

只是编码偏好的问题,但有些人发现这更直观。

In .NET4 onwards you can also use:

Guid myGuid = Guid.Parse(myGuidString); 

Just a matter of coding preference, but some people find this more intuitive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文