小编Evi*_*lDr的帖子

为什么Cast(CType/DirectCast)控件与隐式转换

假设我在webforms GridViewRow中有一个控件...

<asp:Literal ID="ltl_abc" runat="server" />
Run Code Online (Sandbox Code Playgroud)

在RowDataBound事件中,我可以使用以下任何方法访问控件.我一直以来一直使用DirectCast:

Protected Sub gv_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gv.RowDataBound
    Select Case e.Row.RowType
        Case DataControlRowType.DataRow
            ' 1) Dim ltl_abc As Literal = DirectCast(e.Row.FindControl("ltl_abc"), Literal)
            ' 2) Dim ltl_abc As Literal = CType(e.Row.FindControl("ltl_abc"), Literal)
            ' 3) Dim ltl_abc As Literal = e.Row.FindControl("ltl_abc")
Run Code Online (Sandbox Code Playgroud)

使用任何特定方法有什么好处吗?我猜DirectCast稍微有点效率,但可能容易出错,但隐式转换是否有任何危险(选项3)?

从历史上看,在我尝试为控件的属性实际赋值之前,我从未见过任何错误,这让我觉得这第一步不是那么重要吗?

请注意,这不是DirectCast与CType的讨论,更多的是关于在这里是否需要施法?

更新清晰

Protected Sub gv_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gv.RowDataBound
    Select Case e.Row.RowType
        Case DataControlRowType.DataRow

            ' This works fine, but no explicit casting is done:
            Dim ltl_abc As …
Run Code Online (Sandbox Code Playgroud)

vb.net asp.net asp.net-4.5

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

如何使用新分支上的文件

我今天刚开始使用visualstudio.com和VS2013(第一次使用源代码控制),并且正在运行一个网站项目.我的结构如下:

MyCompany.VisualStudio.com\DefaultCollection
 - My Site (project)
    - BuildProcessTemplates
    - Site (folder)
       - My Site (folder)
          - Site (branch)
              - subfolders
          - Site Branch 1 (branch - created just now from "Site")
              - subfolders
Run Code Online (Sandbox Code Playgroud)

在解决方案资源管理器中,我只看到"站点"及其各自的文件.如何切换到"Site Branch 1"的工作?我假设我可以"打开现有网站"并在文件系统上找到它,但这是正确的方法吗?我曾希望可以选择使用Source Control Explorer中的文件.

目前(分支前),Solution Explorer反映了我在Control Explorer中看到的内容: 在此输入图像描述

分支(创建"开发"分支)后,如果我单击"开发"分支中的文件,它将在当前项目下的"杂项文件"文件夹下打开,然后导致编译错误: 在此输入图像描述

因此,要忘记处理"主要"并转而使用"开发",我是否必须从VS中"打开网站",或者我可以将"开发"网站添加到当前解决方案(这听起来不正确) ?

version-control tfs branch visual-studio visual-studio-2013

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

在 MVC 未处理异常上从 Global.asax 重定向到共享错误视图

我一直在阅读有关在 ASP.MVC 中执行错误处理的不同方法。我知道try/catch在控制器内,以及[HandleError]在控制器级别。

但是,我正在尝试对应用程序中任何地方可能发生的未处理异常执行全局错误处理(哇 - 我们从没想过用户会这样做!类型的事情)。这样做的结果是向开发人员发送了一封电子邮件:

这工作正常:

protected void Application_Error()
{
    Exception last_ex = Server.GetLastError();
    Server.ClearError();
    // send email here...
    Response.Redirect("/Login/Login");
}
Run Code Online (Sandbox Code Playgroud)

在应用程序的其他地方,如果控制器发生错误,我们有这个逻辑在我们的错误视图中提供友好的错误:

误差模型

namespace My.Models
{
    public class ErrorViewModel
    {
        public string Summary { get; set; }
        public string Description { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

控制器代码

if(somethingBad){
    return View("Error", new ErrorViewModel { Summary = "Summary here", Description = "Detail here" });
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,是否可以重定向到ErrorViewModel从 Global.asax 中传递的错误视图,例如

// send email …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc asp.net-mvc-5

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

对于基于 cookie 的身份验证,这两种 cookie 配置有什么区别?

对于基于 cookie 的身份验证,这两种 cookie 配置有什么区别?

变体1

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();

    // Configure cookie based authentication:
    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(opt =>
            {
                /* validation rules */    
            });
}
Run Code Online (Sandbox Code Playgroud)

变体2

public void ConfigureServices(IServiceCollection services)
{
    services.AddIdentity<AppUser, AppRole>(opt =>
    {
        /* validation rules */
    });

    services.ConfigureApplicationCookie(options =>
    {
        options.LoginPath = new PathString("/User/Login");

        options.Cookie = new CookieBuilder
        {
            Name = "AspNetCoreIdentityExampleCookie",
            HttpOnly = false,
            SameSite = SameSiteMode.Lax,
            SecurePolicy = CookieSecurePolicy.Always
        };

        options.ExpireTimeSpan = TimeSpan.FromMinutes(2);
        options.SlidingExpiration = true;
    });
}
Run Code Online (Sandbox Code Playgroud)

我无法理解其中的区别,我会很高兴听到任何帮助。

authentication cookies identity asp.net-identity asp.net-core

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

使用sp_executesql计算查询结果而不执行查询

是否可以在不执行查询的情况下使用sp_executesql计算查询返回的结果数?我的意思是说:

我有一个过程,在字符串中获取SQL查询.例:

SELECT KolumnaA FROM Users WHERE KolumnaA > 5
Run Code Online (Sandbox Code Playgroud)

我想分配此查询将返回多少结果的计数,并将其存储在变量中,但我不想实际执行查询.

我无法使用此解决方案:

EXECUTE sp_executesql @sql          
SET @allCount = @@rowcount
Run Code Online (Sandbox Code Playgroud)

因为它返回查询结果,除了获取返回行的计数.

sql t-sql

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

使用XML将关系数据导入SQL Server

我一直在使用XML将大量数据导入SQL一段时间,但我想知道是否可以从具有子节点的单个XML文件跨多个表导入数据?

鉴于这个例子:

DECLARE @tbl_makes TABLE (ID int IDENTITY(1,1), makeName nvarchar(100))
INSERT INTO @tbl_makes (makeName) VALUES ('Ford') 
INSERT INTO @tbl_makes (makeName) VALUES ('Jaguar') 

DECLARE @tbl_models TABLE (ID int IDENTITY(1,1), makeID int, modelName nvarchar(100))
INSERT INTO @tbl_models (makeID, modelName) VALUES (1, 'Escort')
INSERT INTO @tbl_models (makeID, modelName) VALUES (1, 'Sierra')
INSERT INTO @tbl_models (makeID, modelName) VALUES (2, 'XK')
INSERT INTO @tbl_models (makeID, modelName) VALUES (2, 'XJS')

SELECT * FROM @tbl_makes m INNER JOIN @tbl_models md ON m.ID = md.makeID 

DECLARE @xml XML …
Run Code Online (Sandbox Code Playgroud)

sql-server sql-server-2012

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

跨两列创建独占OR约束

我有一个有两列的表(其中包括):

NotApplicable  bit
TargetLevel    numeric(5,2)
Run Code Online (Sandbox Code Playgroud)

我需要创建一个约束,以下规则适用:

  1. NotApplicable和TargetLevel不能都是NULL
  2. NotApplicable和TargetLevel都不能都有值
  3. NotApplicable或TargetLevel必须具有值

我想这是一个独家OR场景?我有一段时间回来了,但我现在意识到这不能解释上面的最后一个场景:

ALTER TABLE [dbo].[my_Table] ADD CONSTRAINT [DF_tbl_my_Table_notApplicable] DEFAULT ((0)) FOR [notApplicable]
GO
ALTER TABLE [dbo].[my_Table] WITH CHECK ADD CONSTRAINT [CK_tbl_my_Table] CHECK  ((COALESCE([targetLevel],[notapplicable]) IS NOT NULL))
GO
ALTER TABLE [dbo].[my_Table] CHECK CONSTRAINT [CK_tbl_my_Table]
GO
Run Code Online (Sandbox Code Playgroud)

任何帮助完善这一点将不胜感激.另外,上面的例子中实际需要的最后一个ALTER语句是吗?

t-sql sql-server sql-server-2012

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

AuthenticationProperties.RedirectUri未在Challenge()中传递给Google

在我的网络应用程序中,我已将Google注册为单点登录提供商:

app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions {
    ClientId = "8765.......apps.googleusercontent.com",
    ClientSecret = "Secret"
})
Run Code Online (Sandbox Code Playgroud)

我的应用不允许用户注册/注册(而是他们的帐户由管理员创建,但他们稍后可以将他们的帐户与Google关联).

在我的"使用Google登录"控制器中,我正在尝试发布一个Challenge()重定向到Google.这可能不是正确的方法:

string redirectUri = "http://localhost:55262/SSO/Google/ProcessToken"; // actually created in code, but shown as string for clarity
AuthenticationProperties properties = new AuthenticationProperties();
properties.RedirectUri = Server.UrlEncode(redirectUri);
Context.GetOwinContext().Authentication.Challenge(properties, "Google");
Run Code Online (Sandbox Code Playgroud)

这正确地将用户发送给Google,但Google会出现错误:redirect_uri_mismatch,说:

请求中的重定向URI:http:// localhost:55262/signin-google 与注册的重定向URI不匹配.

我在Google控制面板中的返回URI集合不包含redirect_uri指定之前看到过此错误.

如果我在VS2015中调试,我可以看到redirect_uri属性设置正确AuthenticationProperties,但似乎OWIN/Katana没有将它传递给Google.相反,当我点击Google时,return_uri是OWIN/Katana使用的默认值.我设置的那个被忽略了.

Google请求详细信息似乎证实了这一点:

scope=openid profile email
response_type=code
redirect_uri=http://localhost:55262/signin-google
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?我是否应该Challenge()不允许用户将其本地应用程序帐户与Google相关联?

asp.net asp.net-mvc owin katana google-signin

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

使用Jquery.ajax()时,.catch()不能出错.然后()

我正在从JQuery中获取许多API,并缓存每个API的结果,以便可以在页面中多次重复使用数据,以呈现不同格式的一些仪表板小部件.

问题是如果API返回带有错误的500状态代码,我不想尝试绘制小部件,但是以友好的方式捕获错误.

但是,我无法弄清楚如何.catch使用该JQuery.ajax()功能.看完这里,这里,这里,这里和其他十几个,我到目前为止,但总是得到相同的控制台错误:

TypeError:LoadDataFromApi(...).then(...).catch不是函数

我试图评论代码来解释我在每个阶段都要做的事情.请有人解释为什么整件事.catch对我不起作用.

// Cache object to save API data for re-use
var requestCache = {};

// Get API data and save to cache 
function LoadDataFromApi(apiUrl) {
    if (!requestCache[apiUrl]) {
        var result = $.ajax({
            type: 'GET',
            url: apiUrl,
            dataType: "json",
            statusCode: {
                500: function (xhr) {
                    var err = JSON.parse(xhr.responseText);
                    console.log('Message:' + err.Message);
                    // throw err.Message; // removed because this was always an "uncaught …
Run Code Online (Sandbox Code Playgroud)

javascript ajax jquery promise

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

SkiaSharp SKBitmap.Decode() 值不能为空。(参数“缓冲区”)

我正在尝试从 Blazor InputFile 组件上传多个图像:

<InputFile OnChange="@LoadFiles" multiple accept=".png, .jpg, .jpeg, .gif" />
Run Code Online (Sandbox Code Playgroud)

一旦进入内存,我需要使用 SkiaSharp 将它们的大小调整为最大宽/高 1000 像素。我为此创建了一个辅助方法,但它似乎在任何超过大约 4MB 的图像上失败。我通过搜索以及任何可用的(相当缺乏的)Microsoft 文档将以下逻辑拼凑在一起,因此我的方法可能完全错误:

using SkiaSharp;

public static byte[] Resize(byte[] fileContents, int maxWidth, int maxHeight)
{
    using MemoryStream ms = new(fileContents);
    using SKBitmap sourceBitmap = SKBitmap.Decode(ms); // <-- EXCEPTION HERE ON LARGER FILES
                        
    int height = Math.Min(maxHeight, sourceBitmap.Height);
    int width = Math.Min(maxWidth, sourceBitmap.Width);
    var quality = SKFilterQuality.High;

    using SKBitmap scaledBitmap = sourceBitmap.Resize(new SKImageInfo(width, height), quality);
    using SKImage scaledImage = SKImage.FromBitmap(scaledBitmap);
    using SKData data = …
Run Code Online (Sandbox Code Playgroud)

c# memorystream skiasharp blazor-server-side .net-6.0

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