JavaScript有parseInt()
和parseFloat()
,但没有parseBool
或parseBoolean
方法在全球范围内,据我所知.
我需要一个方法,使用"true"或"false"之类的值来获取字符串并返回JavaScript Boolean
.
这是我的实现:
function parseBool(value) {
return (typeof value === "undefined") ?
false :
// trim using jQuery.trim()'s source
value.replace(/^\s+|\s+$/g, "").toLowerCase() === "true";
}
Run Code Online (Sandbox Code Playgroud)
这是一个很好的功能吗?请给我你的反馈.
谢谢!
从ActionFilter中止/取消操作的最佳方法
我有这个ActionFilter
,并且假设立即结束连接并返回401 Unauthroized:
public class SignInRequired : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// User is verified, continue executing action
if (Acme.Web.CurrentUser != null)
{
return;
}
// End response with 401 Unauthorized
var response = HttpContext.Current.Response;
response.StatusCode = (int)HttpStatusCode.Unauthorized;
response.End();
// Prevent the action from actually being executed
filterContext.Result = new EmptyResult();
}
}
Run Code Online (Sandbox Code Playgroud)
我学会了如何通过在这里设置'context.Result = new EmptyResult()`来取消执行操作,但我不确定这是否是刷新响应和关闭连接的最佳方法.
看看这个VB.NET代码:
list = GeoController.RegionByCountry(country, language)
Region.allByLanguage(key) = list
Run Code Online (Sandbox Code Playgroud)
在C#中,我可以写一行:
Region.allByLanguage[key] =
(list = GeoController.RegionByCountry(country, language))
Run Code Online (Sandbox Code Playgroud)
有没有办法在VB.NET中使这个单行,就像我在C#中一样?
编辑:你们都必须要睡一觉,否则你可能会想得更加困难.
Region.allByLanguage
是一个缓存.
这是上下文:
Dim list As IEnumerable(Of Region)
Dim key = Region.CacheKey(country, language)
If Region.allByLanguage.ContainsKey(key) Then
list = Region.allByLanguage(key)
Else
list = GeoController.RegionsByCountryAndLanguage(country, language)
Region.allByLanguage(key) = list
End If
Return list
Run Code Online (Sandbox Code Playgroud)
你怎么能告诉我那不是冗长的代码?战栗.
哎呀,如果这是一个C#团队,我只想写:
return Region.allByLanguage.ContainsKey(key) ?
Region.allByLanguage[key] :
(Region.allByLanguage[key] = GeoController.RegionsByCountryAndLanguage(country, language));
Run Code Online (Sandbox Code Playgroud)