C#与.NET技术
实践与教程

专注分享C#、ASP.NET Core、Azure、Blazor等微软技术栈的开发教程、架构设计和最佳实践

85
技术文章
42
教程系列
2.8k
月访问量
328
活跃读者
T4模板 生成多个文件 映射整个库表
2019-03-11 0k

T4模板 生成多个文件 映射整个库表

<#@ output extension="txt" #> <#@ template debug="false" hostspecific="true" language="C#" #> <#@ assembly name="System.Data" #> <#@ assembly name="System.xml" #> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="System.Data.SqlClient" #> <#@ import namespace="System.Data" #> <#@ assembly name="System.Core" #> <#@ import namespace="System.Linq" #> <#@ include file="MultiOutput.tt" #> <# string connectionString= "server=.;database=57blogDB;uid=sa;pwd=123;"; SqlConnection conn = new SqlConnection(connectionString); conn.Open(); string selectQuery ="SET FMTONLY ON; select * from @tableName; SET FMTONLY OFF;"; SqlCommand command = new SqlCommand(selectQuery,conn); SqlDataAdapter ad = new SqlDataAdapter(command); System.

dotnet T4模板 代码生成
阅读更多
把C#程序(含多个Dll)合并成一个exe的超简单方法
2019-03-11 0k

把C#程序(含多个Dll)合并成一个exe的超简单方法

开发程序的时候经常会引用一些第三方的DLL,然后编译生成的exe文件就不能脱离这些DLL独立运行了。 但是,很多时候我们本想开发一款只需要一个exe就能完美运行的小工具。那该怎么办呢? 下文介绍一种超简单的方法,不用写一行代码就可轻松实现。 这里我们需要用到一款名为Fody.Costura的工具。Fody.Costura是一个Fody框架下的插件,可通过Nuget安装到VS工程中。安装之后,就可以将项目所依赖的DLL(甚至PDB)文件全部打包到EXE文件里。 使用方法 在VS中,通过Nuget为目标EXE工程安装Costura.Fody。 重新构建项目。 构建完成后,到项目的输出目录下找到新生成的EXE文件,你同时会发现输出目录下仍然存在那些DLL。不过不用担心,这个EXE已经能够独立运行了。你可以把这些DLL全部删除后再运行EXE试试。 另外,Fody.Costura还支持一些进阶的特性,例如: 临时程序集文件:在运行EXE前自动,自动将DLL从EXE中解压到文件夹系统中,再通过常规的方式加载该DLL。 合并非托管的DLL:Fody.Costura可以合并非托管的DLL,但是不会自动合。如果你的程序涉及非托管DLL,那么你需要通过修改Fody.Costura的配置文件来显示地告诉它你想合并哪些非托管的DLL。 预加载DLL:Fody.Costura可以帮助你在程序启动时预先加载某些DLL,你甚至可以指定这些DLL的加载顺序。 以上这些进阶特性都需要你通过修改Fody.Costura的配置文件来实现,具体的操作步骤可以参考它的官方文档。 好了,Fody.Costura的使用方式已经介绍完了。如果你对Fody.Costura的实现原理感到好奇,可以接着往下看。 实现原理介绍 当CLR试图加载一个程序集但加载失败时,它会引发AppDomain.AssemblyResolve事件。我们的程序可以监听这个事件,并且在这个事件的处理函数中返回这个CLR试图加载的程序集,从而使程序得以继续正常运行。 Fody.Costura在构建项目时会把EXE引用到的DLL全部嵌入到EXE文件中。当程序在运行的过程中用到其中某个DLL的时候(此时由于CLR无法找到该DLL文件,导致AppDomain.AssemblyResolve事件被触发)再从EXE文件的嵌入资源中提取所需的DLL。 下面这两个函数就是Fody.Costura实现这部分逻辑的代码。 public static void Attach() { var currentDomain = AppDomain.CurrentDomain; currentDomain.AssemblyResolve += (s, e) => ResolveAssembly(e.Name); } public static Assembly ResolveAssembly(string assemblyName) { if (nullCache.ContainsKey(assemblyName)) { return null; } var requestedAssemblyName = new AssemblyName(assemblyName); var assembly = Common.ReadExistingAssembly(requestedAssemblyName); if (assembly != null) { return assembly; } Common.Log("Loading assembly '{0}' into the AppDomain", requestedAssemblyName); assembly = Common.

dotnet C# Fody.Costura
阅读更多
DotNet Core AutoMapper实体映射学习记录
后端
2018-03-11 0k

DotNet Core AutoMapper实体映射学习记录

AutoMapper:实体间的映射(数据库Model转Dto) 1、创建Model using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace AutoMapperDemo { public class Entity { } public class PostModel { public Guid Id { get; set; } public long SerialNo { get; set; } public string Title { get; set; } public string Author { get; set; } public string Image { get; set; } public short CategoryCode { get; set; } public bool IsDraft { get; set; } public string Content { get; set; } public DateTime ReleaseDate { get; set; } public virtual IList<CommentModel> Comments { get; set; } } public class CommentModel { public Guid Id { get; set; } public string Email { get; set; } public string Content { get; set; } public DateTime CommentDate { get; set; } } } 2、创建Dto using System; using System.

dotnet AutoMapper 实体映射
阅读更多
sitemap和sitemapindex
2017-05-17 0k

sitemap和sitemapindex

介绍: https://support.google.com/webmasters/answer/75712?hl=zh-Hans 实例: http://www.thepaper.cn/sitemap.xml <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap> <loc>http://www.thepaper.cn/sitemap-1.xml</loc> <lastmod>2017-10-13</lastmod> </sitemap> <sitemap> <loc>http://www.thepaper.cn/asktopic.xml</loc> <lastmod>2017-10-13</lastmod> </sitemap> <sitemap> <loc>http://www.thepaper.cn/newsDetail-7.xml</loc> <lastmod>2017-10-13</lastmod> </sitemap> <sitemap> <loc>http://www.thepaper.cn/newsDetail-6.xml</loc> <lastmod>2017-10-13</lastmod> </sitemap> <sitemap> <loc>http://www.thepaper.cn/newsDetail-5.xml</loc> <lastmod>2017-10-13</lastmod> </sitemap> <sitemap> <loc>http://www.thepaper.cn/newsDetail-4.xml</loc> <lastmod>2017-10-13</lastmod> </sitemap> <sitemap> <loc>http://www.thepaper.cn/newsDetail-3.xml</loc> <lastmod>2017-10-13</lastmod> </sitemap> <sitemap> <loc>http://www.thepaper.cn/newsDetail-2.xml</loc> <lastmod>2017-10-13</lastmod> </sitemap> <sitemap> <loc>http://www.thepaper.cn/newsDetail-1.xml</loc> <lastmod>2017-10-13</lastmod> </sitemap> </sitemapindex>

SEO sitemap sitemapindex
阅读更多
jqurey datatable  tableTools  自定义button元素 以及按钮定义事件
前端
2017-05-13 0k

jqurey datatable tableTools 自定义button元素 以及按钮定义事件

版本 1.10.4 "dom": 'T<"clear">lfrtip', "tableTools": { //"sSwfPath": "~/Resources/Scripts/plugins/DataTables/swf/copy_csv_xls_pdf.swf" "sRowSelect": "multi", "aButtons": [ //{ "sExtends": "new_record", "sButtonText": "Add" }, { "sExtends": "select", "sButtonText": "Delete Recods", "fnClick": function (nButton, oConfig, oFlash) { //delete stuff comes here alert('test'); } } ] } 全部代码: @Styles.Render("~/bundles/css/datatable") @*<script src="~/Resources/Scripts/plugins/DataTables/js/dataTables.tableTools.js"></script> <link href="~/Resources/Scripts/plugins/DataTables/css/data-table.css" rel="stylesheet" />*@ <div> <ol class="breadcrumb pull-right"> @if (ViewBag.Breadcrumbs != null) { var dict = ViewBag.Breadcrumbs as Dictionary<string, string[]>; foreach (var dictItem in dict) { if (dictItem.

前端 jQuery DataTable
阅读更多
DotNet Framework 类库 自带的缓存 HttpRuntime.Cache HttpContext.Cache
后端
2017-03-11 0k

DotNet Framework 类库 自带的缓存 HttpRuntime.Cache HttpContext.Cache

系统自带两个cache类 在.NET运用中经常用到缓存(Cache)对象。有HttpContext.Current.Cache以及HttpRuntime.Cache,HttpRuntime.Cache是应用程序级别的,而HttpContext.Current.Cache是针对当前WEB上下文定义的。HttpRuntime下的除了WEB中可以使用外,非WEB程序也可以使用。 HttpRuntime.Cache 相当于就是一个缓存具体实现类,这个类虽然被放在了 System.Web 命名空间下了。但是非 Web 应用也是可以拿来用的。 HttpContext.Cache 是对上述缓存类的封装,由于封装到了 HttpContext ,局限于只能在知道 HttpContext 下使用,即只能用于 Web 应用。 综上所属,在可以的条件,尽量用 HttpRuntime.Cache ,而不是用 HttpContext.Cache 。 缓存数据规则 数据可能会被频繁的被使用,这种数据可以缓存。 数据的使用频率不高,但 生存周期很长,这样的数据也可以加入缓存。 封装Helper /* * 日期:2016-06-27 */ using System; using System.Web; using System.Collections; public class CacheHelper { /**/ /// <summary> /// 获取数据缓存 /// </summary> /// <param name="CacheKey">键</param> public static object GetCache(string CacheKey) { System.Web.Caching.Cache objCache = HttpRuntime.

dotnet 缓存 缓存机制
阅读更多
DotNet IIS web.config配置文件说明详解
2017-03-11 0k

DotNet IIS web.config配置文件说明详解

keywords : [“web.config配置详解”,“ASP.NET配置文件”,“IIS服务器配置”,“身份验证配置”,“会话状态设置”,“Forms身份验证”,“授权规则配置”,“URL重写模块”] <?xml version="1.0"?> <!--注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的“网站”->“Asp.Net 配置”选项。 设置和注释的完整列表在 machine.config.comments 中,该文件通常位于 "Windows"Microsoft.Net"Framework"v2.x"Config 中。--> <!--Webconfig文件是一个xml文件,configuration是xml文件的根节点,由于xml文件的根节点只能有一个,所以Webconfig的所有配置都是在这个节点内进行的。--> <configuration> <!--指定配置节和命名空间声明。 clear:移除对继承的节和节组的所有引用,只允许由当前 section 和 sectionGroup 元素添加的节和节组。 remove:移除对继承的节和节组的引用。 section:定义配置节处理程序与配置元素之间的关联。 sectionGroup:定义配置节处理程序与配置节之间的关联。--> <configSections> <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/> </sectionGroup> </sectionGroup> <section name="rewriter" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" /> </configSections> <!--appSettings是应用程序设置,可以定义应用程序的全局常量设置等信息--> <appSettings> <add key="1" value="1" /> <add key="gao" value="weipeng" /> </appSettings> <!

配置文件 IIS ASP.NET
阅读更多
HTTP协议状态码详解 HTTP Status Code
前端
2017-03-11 0k

HTTP协议状态码详解 HTTP Status Code

当浏览者访问一个网页时,浏览者的浏览器会向网页所在服务器发出请求。当浏览器接收并显示网页前,此网页所在的服务器会返回一个包含HTTP状态码的信息头(server header)用以响应浏览器的请求。 HTTP状态码的英文为HTTP Status Code。 下面是常见的HTTP状态码: 200 :请求成功。 400 :请求有语法错误。 401 :未授权。 301 :永久转移。 302 :临时转移。 403 :拒绝服务,可能是没有权限等等。 404 :资源没找到。 500 :服务器内部错误。 503 :服务不可用。 1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态代码。 100 (继续) 请求者应当继续提出请求。 服务器返回此代码表示已收到请求的第一部分,正在等待其余部分。 101 (切换协议) 请求者已要求服务器切换协议,服务器已确认并准备切换。 2xx (成功) 表示成功处理了请求的状态代码。 200 (成功) 服务器已成功处理了请求。 通常,这表示服务器提供了请求的网页。 201 (已创建) 请求成功并且服务器创建了新的资源。 202 (已接受) 服务器已接受请求,但尚未处理。 203 (非授权信息) 服务器已成功处理了请求,但返回的信息可能来自另一来源。 204 (无内容) 服务器成功处理了请求,但没有返回任何内容。 205 (重置内容) 服务器成功处理了请求,但没有返回任何内容。 206 (部分内容) 服务器成功处理了部分 GET 请求。 3xx (重定向) 表示要完成请求,需要进一步操作。 通常,这些状态代码用来重定向。

http HTTP状态码 网络协议
阅读更多