我知道在使用XMLHttpRequest时,你不能拦截重定向或阻止重定向,因为浏览器会透明地跟随它,但它是否可能
A.确定重定向的请求,或
B.确定在那里它重定向到?(假设响应没有给出提示)
示例代码:
$.post("/my-url-that-redirects/", {},
function(response, statusCode, xmlHttpRequest){
//Somehow grab the location it redirected to
}
);
Run Code Online (Sandbox Code Playgroud)
在我的情况下,firebug将首先显示对URL的POST,然后对重定向的URL进行GET.可以捕获GET位置吗?
最近,我的同事向我展示了一段无法正常工作的代码:
public class SomeClass
{
private IList<Category> _categories;
public void SetCategories()
{
_categories = GetCategories() ?? new List<Category>();
DoSomethingElse();
}
public IList<Category> GetCategories()
{
return RetrieveCategories().Select(Something).ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
(我知道运算符是多余的,因为linq ToList将始终返回一个列表,但这就是代码的设置方式).
问题是_categories为null.在调试器中,设置断点_categories = GetCategories() ?? new List<Category>(),然后单步执行DoSomethingElse(),_ categories仍然为null.
直接将_categories设置为GetCategories()工作正常.拆分?在一个完整的if语句工作正常.空合并运算符没有.
它是一个ASP.NET应用程序,所以一个不同的线程可能会干扰,但它在他的机器上,只有他在浏览器中连接._cateogories不是静态的,或任何东西.
我想知道的是,这怎么可能发生?
编辑:
只是为了增加奇异性,除了初始化类_categories之外,永远不会设置除该功能之外的任何地方.
确切的代码是这样的:
public class CategoryListControl
{
private ICategoryRepository _repo;
private IList<Category> _categories;
public override string Render(/* args */)
{
_repo = ServiceLocator.Get<ICategoryRepository>();
Category category = _repo.FindByUrl(url);
_categories = _repo.GetChildren(category) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Cubic Hermite Splines绘制图形.我从这个插值方法页面中抓取了这样做的简单代码.
这是我的代码:
private float HermiteInterpolate(float y0, float y1, float y2, float y3, float mu)
{
var mu2 = mu * mu;
var a0 = -0.5f * y0 + 1.5f * y1 - 1.5f * y2 + 0.5f * y3;
var a1 = y0 - 2.5f * y1 + 2f * y2 - 0.5f * y3;
var a2 = -0.5f * y0 + 0.5f * y2;
var a3 = y1;
return (a0 * mu * …Run Code Online (Sandbox Code Playgroud) 我们有一个插件系统,其中插件代码在主进程的单独AppDomain上运行,使用.NET远程处理对象进行通信.
一个类类似于HttpContext.Current(也遇到问题)(编辑,实际实现):
public class MyClass
{
public static MyClass Instance
{
get
{
if(HttpContext.Current != null)
return HttpContext.Current.Items["MyClassInstance"];
}
set
{
if(HttpContext.Current != null)
HttpContext.Current.Items["MyClassInstance"] = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我们有一个继承MarshalByRefObject的通信对象:
public class CommunicatingClass : MarshalByRefObject, ICommunicatingClass
{
public void DoSomething()
{
MyClass.Instance.DoSomething();
}
}
Run Code Online (Sandbox Code Playgroud)
CommunicatingClass是在主AppDomain上创建的,并且工作正常.然后,就是在其AppDomain上创建的插件类,并给出了CommunicatingClass的一个实例:
public class PluginClass
{
public void DoSomething(ICommunicatingClass communicatingClass)
{
communicatingClass.DoSomething();
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,即使CommunicatingClass驻留在主appdomain上(使用立即窗口验证),所有静态数据(如MyClass.Instance和HttpContext.Current)都已消失,并且为null.我有一种感觉MyClass.Instance以某种方式从插件AppDomain中检索,但我不确定如何解决这个问题.
我看到了另一个提出的问题RemotingServices.Marshal,但这似乎没有帮助,或者我错误地使用了它.有没有一种方法可以让CommunicatingClass像主AppDomain中的任何其他类一样访问所有静态方法和属性?
编辑:
PluginClass给出了这样一个实例:
public static PluginClass Create()
{
var appDomain = GetNewAppDomain();
var instance = (PluginClass)appDomain.CreateInstanceAndUnwrap(assembly, type);
instance.Communicator = …Run Code Online (Sandbox Code Playgroud) 我们的应用程序中有一个非常常见的对象.在这种情况下,我们称之为Ball.球很好,但在某些配置中,它们的行为不同.它目前设置如下:
class Ball
{
private static readonly bool BallsCanExplode;
static Ball()
{
bool.TryParse(ConfigurationManager.AppSettings["ballsCanExplode"],
out BallsCanExplode);
}
public Ball(){}
}
Run Code Online (Sandbox Code Playgroud)
这在实践中完全正常.如果配置是球可以爆炸,它们会爆炸,如果不是,则不会爆炸.问题是它完全不可测试.我无法找到一种保持可测试性的好方法,并且仍然易于实例化.
最简单的解决方案是将球和配置分离:
class Ball
{
private readonly bool CanExplode;
public Ball(bool canExplode);
}
Run Code Online (Sandbox Code Playgroud)
这个问题是曾经在Ball类中曾经是一个孤立的依赖,现在已经扩散到每个制作Ball的类中.如果这种依赖注入,那么爆炸球的知识必须到处注入.
BallFactory存在同样的问题.虽然每个班级都可以去new Ball(),但现在必须知道必须在任何地方注入的BallFactory.另一种选择是使用已经在应用程序中烘焙的服务定位器:
class Ball
{
private readonly bool CanExplode;
public Ball()
{
CanExplode = ServiceLocator.Get<IConfiguration>().Get("ballsCanExplode");
}
}
Run Code Online (Sandbox Code Playgroud)
这仍然保持了球的配置依赖性,但允许注入测试配置.虽然使用了球,但是在每次new Ball()调用时找到服务似乎有点过分.
什么是保持这个可测试的最佳方法,以及易于实例化?
注意:应用程序中既有依赖注入框架又有服务定位器,它们都经常使用.