小编Ali*_*ned的帖子

后退按钮不会导致回发到MVC中的控制器操作

当我点击IE10中的后退按钮或Win7上的Chrome时,它没有达到我在MVC控制器中的断点.IE开发人员工具中的"网络"选项卡显示其未修改304且Fiddler未捕获请求.

我期待回帖,所以我可以在我的控制器中工作.就我而言,错误是:

  1. 登入
  2. 确保您在默认页面上
  3. 单击左上角的浏览器后退按钮,您现在将返回登录屏幕
  4. 当你这样做时再次使用相同的凭据登录 - 我得到"提供的防伪令牌是为了用户",但当前用户是"用户名".

我试过把它放在我的控制器中,没有成功:

this.HttpContext.Response.CacheControl = "private";
this.HttpContext.Response.Cache.SetMaxAge(TimeSpan.FromSeconds(0));
public ActionResult Index()
{
    // Get: /Home/Index
    if (this.User.Identity.IsAuthenticated)
    {
        // send the user to the GlobalAssetDashboard
        return this.RedirectToAction(
            "GlobalAssetDashboard",
            "Dashboard",
            new
                {
                    area = "DashboardArea"
                });
    }

    return this.View("Login");
}
 public ActionResult Login()
{
    // GET: /Home/Login
    if (this.User.Identity.IsAuthenticated)
    {
        // send the user to the GlobalAssetList
        return this.RedirectToAction(
            "GlobalAssetDashboard",
            "Dashboard",
            new
                {
                    area = "DashboardArea"
                });
    }

    return this.View("Login", new LoginModel());
}
Run Code Online (Sandbox Code Playgroud)

有没有办法强制回发或检测到这种情况并导致JavaScript刷新?或者我的控制器方法实现不正确?

asp.net-mvc postback

6
推荐指数
1
解决办法
4048
查看次数

Assert.ThrowsExceptionAsync不起作用

问题:我没有太多关于MSTest V2的示例或文档.使用Assert.ThrowsExceptionAsync的正确方法是什么?

public void GetPlaylistByIdAsync_NonExistingPlaylist_ThrowsPlaylistNotFoundException()
{
    var playlistId = Guid.NewGuid().ToString();
    var manager = PlaylistTargetsFakeFactory.GetPlaylistTargetFusionManager();
    Assert.ThrowsException<PlaylistNotFoundException>(async () =>
    {
       await manager.GetPlaylistByIdAsync(playlistId);
    });
}
Run Code Online (Sandbox Code Playgroud)

这也未通过测试:

Assert.ThrowsException<PlaylistNotFoundException>(() =>
{
    return manager.GetPlaylistByIdAsync(playlistId);
});
Run Code Online (Sandbox Code Playgroud)

"消息:Assert.ThrowsException失败.没有抛出异常.预计会出现PlaylistNotFoundException异常."

这对我来说是失败的,即使我已经调试它并且确实抛出了异常.

由于这仍然是RC,因此可能存在错误.我在2次测试中有这个我试图转换所以我可以使用VS 2017.

更新:通过.

[TestMethod]
public async Task GetPlaylistByIdAsync_NonExistingPlaylist_ThrowsPlaylistNotFoundException()
{
    var playlistId = Guid.NewGuid().ToString();
    var manager = PlaylistTargetsFakeFactory.GetPlaylistTargetFusionManager();
    //Assert.ThrowsException<PlaylistNotFoundException>(() =>
    //{

    try
    {
        await manager.GetPlaylistByIdAsync(playlistId);
        Assert.Fail();
    }
    catch (PlaylistNotFoundException)
    {
        Assert.IsTrue(true);
    }

    //});
}
Run Code Online (Sandbox Code Playgroud)

更新2:在Stephen Cleary的回答之后,我做了这个改变.谢谢你指出我的误用.我已经改变了一段时间,因为我得到"消息:测试方法Daktronics.UserPortal.Test.Models.Helpers.PlaylistTargetFusionManagerTests.GetPlaylistByIdAsync_NonExistingPlaylist_ThrowsPlaylistNotFoundException引发异常:System.MissingMethodException:找不到方法:'System.Threading.Tasks.Task 1<!!0> Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsExceptionAsync(System.Func1)'. " 当我运行测试.

[TestMethod]
[TestCategory(TestCategories.CSharp)]
[TestCategory(TestCategories.PlaylistTargets)] …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing mstest

6
推荐指数
1
解决办法
2898
查看次数

如何在Silverlight中评估XPath表达式?

我需要允许高级用户输入XPath表达式并向他们显示找到的值或节点或属性.在.Net框架中,System.Xml.XPath.Extensions可用于调用XPathEvaluate,但Silverlight没有此MSDN引用.有没有人重写过在Silverlight中使用的扩展方法?最好的方法是什么?为什么它们不能在Silverlight或工具包中使用(在此处对此问题进行投票)?

xml silverlight xpath

5
推荐指数
1
解决办法
1878
查看次数

访问ASP.NET Core静态主中的IHostingEnvironment

我正在尝试在我升级的Asp.Net Core RC2应用程序的静态void main中获取配置值.在Startup的构造函数中,我可以注入IHostingEnvironment,但不能在静态方法中执行此操作.我正在关注https://github.com/aspnet/KestrelHttpServer/blob/dev/samples/SampleApp/Startup.cs,但想在appsettings中使用我的pfx密码(是的,它应该在用户机密中并且会到达那里最终).

public Startup(IHostingEnvironment env){}

public static void Main(string[] args)
{
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddJsonFile("hosting.json");
        builder.AddEnvironmentVariables();
        var configuration = builder.Build();
       ...
       var host = new WebHostBuilder()
            .UseKestrel(options =>
            {
                // options.ThreadCount = 4;
                options.NoDelay = true;
                options.UseHttps(testCertPath, configuration["pfxPassword"]);
                options.UseConnectionLogging();
            })
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-1.0

5
推荐指数
2
解决办法
5377
查看次数

如何为Kestrel响应添加no-cache?

我使用Asp.Net Core RC2和Kestrel作为我的Web服务器.我需要确保使用no-cache标头响应请求(在这种情况下所有这些请求),以便浏览器获得最新版本(而不是304).

在Startup中有没有办法配置Kestrel或将此步骤注入管道?

编辑:在我的情况下,no-store可能是更好的选择:https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching "no-store不允许缓存响应并且必须在每次请求时全部取出."

asp.net kestrel-http-server asp.net-core

4
推荐指数
1
解决办法
1621
查看次数

如何使用XDocument.XPathEvaluate处理空命名空间?

我正在尝试使用XDocument和XPathEvaluate从woot.com提要中获取值.我正在处理其他名称空间,但这个例子给了我一些问题.

     <rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
      <channel>    
        <category text="Comedy" xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd">
</category> 
<!-- this is a problem node, notice 'xmlns=' --!>
Run Code Online (Sandbox Code Playgroud)

所以我试试这个:

  XmlNamespaceManager man = new XmlNamespaceManager(nt);
    man.AddNamespace("ns", "http://www.w3.org/2000/xmlns/");
// i've also tried man.AddNamespace("ns", string.Empty);
    xDocument.Namespace = man;
    var val = xDocument.XPathEvaluate("/rss/channel/ns:category/@text", xDocument.Namespace);
Run Code Online (Sandbox Code Playgroud)

val始终为null.我正在使用ns:来自VS 2010 XPath Navigator插件的建议.有关如何处理这个问题的任何想法?

.net xpath linq-to-xml xml-namespaces

2
推荐指数
1
解决办法
1209
查看次数

如何使用expand()为具有WCF Data Services for Silverlight的实体包含多个级别的预先加载的属性?

我正在使用Silverlight 4.0(所以我需要调用异步并且不能直接使用EF)和WCF数据服务以及EF 4来建模数据库.我想进行一次调用并填充几个级别的属性.

假设我有以下设置(但这可能更深入):帐户 - 拥有零个或多个客户(和其他属性) - 客户拥有零个或多个地址(和其他属性)

我想带回1个有效负载,其中Accounts,Customers和Addresses都是急切加载并包含在那个有效负载中.

我想得到:Accounts.Expand("Customers").其中(a => a.Id == 1); 这将返回帐户和客户填充的有效内容.如何在同一个呼叫中包含地址?

wcf-data-services silverlight-4.0

1
推荐指数
1
解决办法
1561
查看次数

在Xamarin中跨项目共享图像

我想在Xamarin的多个设备应用程序中使用相同的图像.

我有一个PCL核心库,并已开始使用MVVMCross.实现这一目标的最佳方法是什么?我可以将它们作为资源嵌入到Core库中并在我的Andriod/iOs项目中访问它,还是使用add作为链接更好?

mvvmcross xamarin

1
推荐指数
1
解决办法
1201
查看次数

jQuery和CORS

如果服务器没有响应JSONP,我该如何处理?我已尝试使用$ .ajax的几个选项,但无法使其工作.

CodePen示例

//var url = 'https://coinbase.com/api/v1/currencies/exchange_rates';
var url = 'http://blockchain.info/ticker';
$.ajax({
    type: 'GET',
    url: url,
    //jsonpCallback: 'jsonCallback',
    contentType: 'application/json',
    //async: false,
    //dataType: 'jsonp',

    //xhrFields: { 
    // for CORS?
    //  withCredentials: false
    //},
    success: function (json) {
        debugger;
    },
    error: function (json) {
     debugger;
    }
});
Run Code Online (Sandbox Code Playgroud)

ajax jsonp cors

1
推荐指数
1
解决办法
2255
查看次数