ASP.NET中这些类之间有什么区别?正如我发现这些类之间没有继承关系.
下面的代码返回一个实例HttpRequestWrapper
,其is a
HttpRequestBase
与has a
HttpRequest
HttpRequestMessage request = ...;
HttpRequestBase reqBase = (request.Properties["MS_HttpContext"] as HttpContextWrapper).Request;
// do somthing with reqBase.Cookies
Run Code Online (Sandbox Code Playgroud)
看起来微软希望在从HttpRequestMessage获取cookie时惹恼我们.
是否保证request.Properties["MS_HttpContext"]
永远不会为空?
或者认为在ApiController的动作中处理ajax请求.我可以通过两种不同的方式达到客户端的IP.
var ip = (request.Properties["MS_HttpContext"] as HttpContextWrapper).Request.UserHostAddress
var ip = HttpContext.Current.Request.UserHostAddress
Run Code Online (Sandbox Code Playgroud)
这两者有什么区别?
或者一般来说,我可以以不同的方式访问相同的请求/响应数据,例如Cookie,Header,Requestor Info等.什么时候用哪个?我们可以说"如果它是ajax请求,HttpRequest不能保证正常工作,因为缺少某些东西所以对于ajax请求我们应该使用HttpRequestMessage"吗?
我篡改了表达式,我在某些方面感到困惑
我们可以将相同的LamdaExpression分配给Expression和/或Func.但我们不能将Func分配给表达式(或表达式为Func).为什么我们不能这样做?我查找是否定义了Expression和Func之间的转换运算符但我找不到任何转换运算符.
Func<int, int> sumFunc = i => i + i;
Expression<Func<int, int>> sumExp = i => i + i;
// sumExp = sumFunc; // Cannot convert source type 'System.Func<int,int>' to target type 'System.Linq.Expressions.Expression<System.Func<int,int>>'
// sumFunc = sumExp; // Cannot convert source type 'System.Linq.Expressions.Expression<System.Func<int,int>>' to target type 'System.Func<int,int>'
Run Code Online (Sandbox Code Playgroud)即使我们无法将LambdaExpression分配给对象.再说一遍,为什么我们不能这样做呢?
// object o = i => i + i; // Cannot convert source type 'lambda expression' to target type 'object'
Run Code Online (Sandbox Code Playgroud)我认为编译器有一些东西.如果是这样,我们可以编写那些以这种(令人困惑的)方式表现的自定义类型并利用某些东西.
我试图使用ui-router多个可选参数但它似乎不起作用.以下是我做的测试:
单个参数可以
state.url url called state param values
/page/:x /page/ $stateParams.x == ""
/page/:x /page/2 $stateParams.x == "2"
Run Code Online (Sandbox Code Playgroud)
一个可选参数是OK
/page/:x? /page/ $stateParams.x == ""
/page/:x? /page/2 $stateParams.x == "2"
Run Code Online (Sandbox Code Playgroud)
两个参数都OK了(除了在第一种情况下丑陋的双斜线,/page
并/page/
转成/page//
.但由于参数是不可选的,没关系)
/page/:x/:y /page// $stateParams.x == "" && $stateParams.y == ""
/page/:x/:y /page/2 $stateParams.x == "" && $stateParams.y == ""
/page/:x/:y /page/2/ $stateParams.x == "2" && $stateParams.y == ""
/page/:x/:y /page/2/3 $stateParams.x == "2" && $stateParams.y == "3"
Run Code Online (Sandbox Code Playgroud)
两个可选参数表现很奇怪.第二个参数总是未定义的,当我指定第二个参数时,它不能解决第一个参数.
/page/:x?/:y? /page/ $stateParams.x == "" && $stateParams.y …
Run Code Online (Sandbox Code Playgroud) 我有这个基本设置来构建我的角度模板.
这是 gulpfile
var gulp = require("gulp"),
templateCache = require('gulp-angular-templatecache');
gulp.task("tc", function() {
return gulp
.src("test.html")
.pipe(templateCache()) // when I comment out this line I see test.html file is getting copied under dest folder
.pipe(gulp.dest("dest"));
});
Run Code Online (Sandbox Code Playgroud)
这是一个位于gulpfile旁边的简单html文件.
<div>
Test
</div>
Run Code Online (Sandbox Code Playgroud)
当我运行"gulp tc"时,我得到以下错误.
[17:49:19] Using gulpfile ~SOME_PATH/gulpfile.js
[17:49:19] Starting 'tc'...
fs.js:839
return binding.lstat(pathModule._makeLong(path));
^
Error: ENOENT: no such file or directory, lstat '/SOME_PATH/templates.js'
at Error (native)
at Object.fs.lstatSync (fs.js:839:18)
at DestroyableTransform.TransformStream [as _transform] (/SOME_PATH/node_modules/gulp-angular-templatecache/node_modules/gulp-header/index.js:38:12)
at DestroyableTransform.Transform._read (/SOME_PATH/node_modules/gulp-angular-templatecache/node_modules/gulp-header/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:159:10)
at DestroyableTransform.Transform._write (/SOME_PATH/node_modules/gulp-angular-templatecache/node_modules/gulp-header/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:147:83)
at …
Run Code Online (Sandbox Code Playgroud) 下面是我的问题的简单演示代码.
[TestClass]
public class ExpressionTests
{
[TestMethod]
public void TestParam()
{
Search<Student>(s => s.Id == 1L);
GetStudent(1L);
}
private void GetStudent(long id)
{
Search<Student>(s => s.Id == id);
}
private void Search<T>(Expression<Func<T, bool>> filter)
{
var visitor = new MyExpressionVisitor();
visitor.Visit(filter);
}
}
public class MyExpressionVisitor : ExpressionVisitor
{
protected override Expression VisitConstant(ConstantExpression node)
{
Assert.AreEqual(1L, node.Value);
return base.VisitConstant(node);
}
}
Run Code Online (Sandbox Code Playgroud)
TestParam
方法导致VisitConstant
在两个不同的路径上调用:
1. TestParam
- > Search
- >VisitConstant
在此执行路径中,传递给Search
方法的常量表达式(1L)是实常数值.在这里,一切都很好,断言按预期成功.当VisitConstant
通过第一路径调用node.Value.GetType()
是 …
我想编写C#代码的等效Java代码.
我的C#代码如下:
public abstract class A<T> where T : A<T>, new()
{
public static void Process()
{
Process(new T());
}
public static void Process(T t)
{
// Do Something...
}
}
public class B : A<B>
{
}
public class C : A<C>
{
}
Run Code Online (Sandbox Code Playgroud)
Java代码与我的代码相似.
public abstract class A<T extends A<T>>
{
public static <T extends A<T>> void process()
{
process(new T()); // Error: Cannot instantiate the type T
}
public static <T extends A<T>> void process(T t)
{ …
Run Code Online (Sandbox Code Playgroud) 假设Constructor和Process方法可能抛出异常,使用Disposable对象的最佳方法是什么?我通常更喜欢以下实现之一.
try-catch周围使用块
try
{
using (Disposable dispObj = new Disposable())
{
dispObj.Process();
}
}
catch (Exception ex)
{
// Do something
}
Run Code Online (Sandbox Code Playgroud)try-catch-finally块.
Disposable dispObj2 = null;
try
{
dispObj2 = new Disposable();
dispObj2.Process();
}
catch (Exception ex)
{
// Do something
}
finally
{
if (dispObj2 != null)
{
dispObj2.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)更新:
再说一遍:"假设Constuctor和Process方法可能抛出异常".我真的不明白为什么没有人关心他们答案中的异常.
我希望我的Web应用程序在没有菜单栏,地址栏等的窗口中运行.我有一个Default.html页面,在此页面中我单击"运行应用程序"按钮,首先在另一个窗口中打开我的应用程序window.open
,然后关闭使用window.open
with _self
参数打开"AutoClose.html"的当前窗口
default.html中
<html>
<head>
<script type="text/javascript">
function runApp() {
// Open my application
window.open('Application.html', null, 'status:no;dialogHide:true;help:no;scroll:yes;center=yes;');
// Close this window
window.open('AutoClose.html', '_self');
}
</script>
</head>
<body>
<input type="button" value="Run Application" onclick="runApp();" />
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
AutoClose.html
<html>
<head>
<script type="text/javascript">
window.close();
</script>
</head>
<body></body>
</html>
Run Code Online (Sandbox Code Playgroud)
我的应用程序应支持IE,Firefox和Chrome,我的代码在IE和Chrome上工作正常,但由于"脚本可能无法关闭脚本未打开的窗口"警告,因此无法关闭Firefox上的第一个窗口.在Firefox上它打开AutoClose.html但是window.close()
在此页面中调用只会导致"脚本可能无法关闭脚本未打开的窗口"警告并且窗口未关闭.顺便说一句,我的应用程序窗口打开没有任何问题(没有问题).
看来,使用window.open()
带有_self
参数的伎俩Firefox没有工作.因为我的目标是在没有菜单栏,地址栏等的窗口中运行应用程序.
window.open()
(至少对于Firefox)?然后我不需要跑window.close()
window.close()
在Firefox上有没有办法让"使用javascript的脚本无法打开的窗口"工作?提前致谢.
更新:这是一个银行应用程序,我只是一个开发人员而不是决策者.换句话说,某种分析师希望应用程序以这种方式工作.我不是在质疑它.所以"你要做的全部事情是完全错误的"答案不会真正有用.
我是Linq to Entity的新手,这是我的测试场景:
我想编写一个方法,显示使用Linq在特定用户的相册中使用的标签的计数和名称.
这是我写的方法,它工作正常
public static void TestStatistics(int userId)
{
// select AlbumTitle, TagName, TagCount where UserId = userId
var results = from photo in dbSet.PhotoSet
join album in dbSet.AlbumSet on photo.AlbumId equals album.Id into albumSet
from alb in albumSet
where alb.UserId == userId
join photoTag in dbSet.PhotoTagSet on photo.Id equals photoTag.PhotoId into photoTagSet
from pt in photoTagSet
join tag in dbSet.TagSet on pt.TagId equals tag.Id
group new { alb, tag } by new { …
Run Code Online (Sandbox Code Playgroud) 首先,这不是一个问题,我只是做了一个用户控件,似乎解决了以下链接中讨论的问题.
http://forum.jquery.com/topic/dialog-will-move-its-div-tag-to-body
问题是,简单地说,使用部分页面更新时,JQuery对话框会导致一些问题.从ASP.NET方面来说,当它在UpdatePanel中使用时.
您可以从以下链接中查看示例并下载源代码.
http://www.mbmt.net/JQ/JQDialogTest.aspx http://www.mbmt.net/JQ/JQDialogTest2.aspx
我希望这可以帮助别人 (:
c# ×6
javascript ×3
asp.net ×2
linq ×2
ajax ×1
angularjs ×1
asp.net-mvc ×1
closures ×1
dynamic ×1
expression ×1
firefox ×1
func ×1
generics ×1
gulp ×1
httprequest ×1
idisposable ×1
inheritance ×1
java ×1
jquery ×1
jquery-ui ×1
lambda ×1
modal-dialog ×1
npm ×1
t-sql ×1