首页 > 编程语言 >ASP.NET CORE开发 (三)

ASP.NET CORE开发 (三)

时间:2023-03-31 16:58:48浏览次数:36  
标签:CORE ASP string IntPtr int bool https new NET

1. 在使用singleton时出现 Cannot resolve scoped service 'AlgoTag.Models.AlgoContext' from root provider.

https://www.cnblogs.com/liuzeqi/p/14132325.html

原因:一般情况下,生命周期较长的服务不应该依赖一个生命周期较短的服务,Scope的服务被Singleton的服务依赖/引用,那么这个Scope的服务也会随依赖它的Singleton服务变成Singleton的。

ref: service 有参构造: https://www.cnblogs.com/nickyX/p/14731137.html

2. 在IIS里如何访问Linux系统的共享文件夹

第一种方式: 在代码中实现 https://www.cjavapy.com/article/395/, git源码: https://github.com/zinkpad/SharpCifs   这种方式我没有尝试过。

第二种方式:模拟登录:https://blog.csdn.net/cxu123321/article/details/108592475 我采用了方案一,利用WindowsAPI :

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Runtime.InteropServices;  
  
namespace ConsoleApplication5  {  
    public class SharedTool : IDisposable  {  
        // obtains user token       
        [DllImport("advapi32.dll", SetLastError = true)]  
        static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword,  
            int dwLogonType, int dwLogonProvider, ref IntPtr phToken);  
  
        // closes open handes returned by LogonUser       
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]  
        extern static bool CloseHandle(IntPtr handle);  
  
        [DllImport("Advapi32.DLL")]  
        static extern bool ImpersonateLoggedOnUser(IntPtr hToken);  
  
        [DllImport("Advapi32.DLL")]  
        static extern bool RevertToSelf();  
        const int LOGON32_PROVIDER_DEFAULT = 0;  
        const int LOGON32_LOGON_NEWCREDENTIALS = 9;//域控中的需要用:Interactive = 2       
        private bool disposed;  
  
        public SharedTool(string username, string password, string ip)  
        {  
            // initialize tokens       
            IntPtr pExistingTokenHandle = new IntPtr(0);  
            IntPtr pDuplicateTokenHandle = new IntPtr(0);  
  
            try  
            {  
                // get handle to token       
                bool bImpersonated = LogonUser(username, ip, password,  
                    LOGON32_LOGON_NEWCREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref pExistingTokenHandle);  
  
                if (bImpersonated)  
                {  
                    if (!ImpersonateLoggedOnUser(pExistingTokenHandle))  
                    {  
                        int nErrorCode = Marshal.GetLastWin32Error();  
                        throw new Exception("ImpersonateLoggedOnUser error;Code=" + nErrorCode);  
                    }  
                }  
                else  
                {  
                    int nErrorCode = Marshal.GetLastWin32Error();  
                    throw new Exception("LogonUser error;Code=" + nErrorCode);  
                }  
            }  
            finally  
            {  
                // close handle(s)       
                if (pExistingTokenHandle != IntPtr.Zero)  
                    CloseHandle(pExistingTokenHandle);  
                if (pDuplicateTokenHandle != IntPtr.Zero)  
                    CloseHandle(pDuplicateTokenHandle);  
            }  
        }  
  
        protected virtual void Dispose(bool disposing)  {  
            if (!disposed)  
            {  
                RevertToSelf();  
                disposed = true;  
            }  
        }  
  
        public void Dispose()  
        {  
            Dispose(true);  
        }  
    }  
}  

 

3. 当使用ajax获取到后台返回image/jpeg 的图片时如何在页面上用jquery显示出来

    function GetNextImage(i) {
        $.ajax({
            url: "/Home/GetNextImage/" + i,
            type: "get",
            xhrFields: {
                responseType: 'blob'
            },
            success: function (data) {
                var img = i == 0 ? $("#topimg") : $("#bottomimg");
                var reader = new FileReader();
                reader.onload = function (e) {
                    img.css("background-image", "url(" + e.target.result + ")");
                }
                reader.readAsDataURL(data);
            },
            error: function (e) {
                alert(e);
            }
        });

  

需要设定responseType 为 blob。

如果是直接显示在页面上的话,那么可以把URL直接设给src 属性:

<img style="background-image: url('/Home/Image'); background-position:-324px 0px;" width="1800" height="720"/>

 

4. 下载下来的UTF-8编码格式的csv文件,直接用Excel打开是乱码的问题。

用notepad++ 打开没有问题, 用notepad++ 转换成UTF-8 BOM 保存后再用Excel打开也没有问题。所以在服务器上应该给文件内容加上BOM 头。

引用以下 答案: https://stackoverflow.com/questions/4414088/how-to-getbytes-in-c-sharp-with-utf8-encoding-with-bom

public IActionResult Download()
{
    var data = Encoding.UTF8.GetBytes("some data");
    var result = Encoding.UTF8.GetPreamble().Concat(data).ToArray();
    return new FileContentResult(result, "text/plain;charset=utf-8") { FileDownloadName = "数据汇总.csv" };
}

 

标签:CORE,ASP,string,IntPtr,int,bool,https,new,NET
From: https://www.cnblogs.com/crazyghostvon/p/17129407.html

相关文章

  • Visual Studio创建.net 6的WinForm一直提示"正在加载设计器"
    用VS创建.net6的WinForm,一直停留在"正在加载设计器"的界面*其实不止.net6,也能解决其它非.netframework的WinForm状况 原因1.删除了本地的安装缓存(VS安装时会把安装文件先下载到本地)2.关闭了或没打开NuGet 解决方法1.VS菜单->工具->选项->NuGet包管理器->程......
  • net framwork winform
    winform是窗体项目该winform项目使用的数据库是mysql,vs版本是2022创建一个netframework项目 随便选一个框架版本  将form窗体中加入 一个DataGridView、四个button(新增、更新、删除、查询)按钮、一个textbox。这些控件在 视图---工具栏     把窗体中的......
  • 解决videojs 在Chrome浏览器下报:A network error caused the media download to fail
    记录一下videoJS在Chrome浏览器下有时候出现播放一半或者回退的一个恶心bug,错误提示如下:Anetworkerrorcausedthemediadownloadtofailpart-way.经过一下午的折腾查找,终于在GitHub上看到他们官方的一个解决方案,这个方案目前没有更新在官方文档最新版本中,只是随便提了一下......
  • CSharp: Tesseract OCR V5.0 in donet core 3.1
    Referenceresourceshttps://github.com/alex-doe/open-ocr-dotnethttps://github.com/tleyden/open-ocr/gohttps://github.com/DayBreak-u/chineseocr_litehttps://github.com/pjreddie/darknethttps://sourceforge.net/projects/vietocr/https://github.com/PaddlePaddle/......
  • 基于Go/Grpc/kubernetes/Istio开发微服务的最佳实践尝试 - 3/3
    基于Go/Grpc/kubernetes/Istio开发微服务的最佳实践尝试-1/3基于Go/Grpc/kubernetes/Istio开发微服务的最佳实践尝试-2/3基于Go/Grpc/kubernetes/Istio开发微服务的最佳实践尝试-3/3项目地址:https://github.com/janrs-io/Jgrpc转载请注明来源:https://janrs.com/6rdh......
  • 基于Go/Grpc/kubernetes/Istio开发微服务的最佳实践尝试 - 2/3
    基于Go/Grpc/kubernetes/Istio开发微服务的最佳实践尝试-1/3基于Go/Grpc/kubernetes/Istio开发微服务的最佳实践尝试-2/3基于Go/Grpc/kubernetes/Istio开发微服务的最佳实践尝试-3/3项目地址:https://github.com/janrs-io/Jgrpc转载请注明来源:https://janrs.com/ugj7......
  • base64 之坑----Base64后出现换行符 org.apache.commons.net.util.Base64()
    坑记录问题:base64后的结果会出现\r\n换行符, 复现:publicstaticvoidmain(String[]args){Stringkey="ssjsi21djsiej284858jweejrh34981dwde32323243232423423412121";Strings=java.util.Base64.getEncoder().encodeToString(key.getBytes(St......
  • Asp.Net Framework项目优化前端文件引用
    背景:公司一个老项目,前端引用都是直接引用文件,这样导致每次发布都存在用户浏览器缓存问题,常见做法是找到每个引用前端文件处加?version,但是太麻烦了解决思路:1.找到所有前端文件2.jenkins每次编译更新版本号,把新的版本号拼接到前端文件引用处实现:1.首先在扩展类里面扩展以......
  • Asp.Net Core 动态生成WebApi
    在WebApi架构体系中,一般需要先编写应用服务实现,再通过编写Controller来实现应用服务的Web接口。Controller中的代码作用仅仅是调用Service的方法,将Service提升为Web接口,其实完全可以通过动态生成WebApi来减少编码工作。在.Net示例项目ABP中已经实现了动态生成WebApi的功能,Panda.Dy......
  • Elasticsearch.Net+Nest基本用法
    基本用法安装Nest(安装后默认会装上Elasticsearch.Net),注意:版本尽量选择跟ES版本一致的批量初始化数据到ESusingNest;try{//测试环境配置SSL证书需要的设置ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls12|SecurityProtocolType.Tls11......