我想使用最新版本的ASP.NET Core,因为我想用它创建一组Web API.但是,我发现的教程主要关注实体框架.我无法使用它,因为我已经拥有"遗留"数据库,因此Code-First方法不是一种选择.
我的想法是使用ADO.NET连接到我的数据库,但我不知道是否System.Data.SqlClient在ASP.NET Core项目中可用.我已经发现,当我使用.NET Framework项目模板但是它仍然可以在.NET Core项目中使用时可用吗?
我正在编写一个基本的应用程序来学习ASP.NET 5.我觉得很困惑的一个领域是配置.在ASP.NET 5之前,我可以执行以下操作:
var settingValue = ConfigurationManager.AppSettings["SomeKey"];
Run Code Online (Sandbox Code Playgroud)
我会在我的代码中散布各种代码.现在,在vNext世界中,我有一个如下所示的config.json文件:
config.json
{
"AppSettings": {
"SomeKey":"SomeValue"
}
}
Run Code Online (Sandbox Code Playgroud)
然后在Startup.cs中,我有以下内容: Startup.cs
public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment environment)
{
Configuration = new Configuration()
.AddJsonFile("config.json");
}
Run Code Online (Sandbox Code Playgroud)
从那里,我完全难过.我在/src/Website/Code/Models/MyClass.cs中有MyClass.cs.
MyClass.cs
public class MyClass
{
public string DoSomething()
{
var result = string.Empty;
var keyValue = string.Empty; // TODO: What do I do here? How do I get the value of "AppSettings:SomeKey"?
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
如何获得"AppSettings:SomeKey"的值?
我想让web.config转换在本地工作,但显然只在进行部署时才会发生转换.
有没有人知道如何运行msbuild目标"TransformWebConfig"而不通过"rebuild"进程,还指定并输出目录在哪里吐出转换后的web.config?
编辑:使用Sayed的答案,我创建了一个.bat文件来为我运行任务:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Msbuild.exe "D:\Demo\Transformation.proj" /t:TransformWebConfig
copy /Y "D:\Demo\Web.config" "D:\MyProject\Web.config"
del ""D:\Demo\Web.config"
Run Code Online (Sandbox Code Playgroud)
"Transformation.proj"是Sayed的代码片段的副本,在下面的答案中.只需指定转换的源,目标和目标.新文件(在本例中为已转换的"web.config")将位于"D:\ Demo"目录中.我只是将其复制以覆盖我的项目的web.config,最后,删除"demo"文件夹中生成的文件.
另外,我创建了一个宏来运行这个批处理文件并为我执行转换:
Public Module DoTransform
Sub RunTransformBatchFile()
Try
Process.Start("D:\Demo\RunTransform.bat")
Catch ex As System.Exception
MsgBox(ex.Message)
End Try
End Sub
End Module
Run Code Online (Sandbox Code Playgroud)
您还可以在工具栏上添加一个按钮来运行此批处理和/或指定要执行的快捷键.
asp.net web-config visual-studio-2010 web-deployment msdeploy
对于我正在开发的项目,我们正在实现的一件事是我们的一些团队的旧ASP.NET和MVC项目中的代码 - 一个Application_Error例外捕获器,它向开发团队发送一封例外的电子邮件经验和最相关的细节.
以下是它的外观:
Global.asax中:
protected void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
string path = "N/A";
if (sender is HttpApplication)
path = ((HttpApplication) sender).Request.Url.PathAndQuery;
string args = string.Format("<b>Path:</b> {0}", path);
// Custom code that generates an HTML-formatted exception dump
string message = Email.GenerateExceptionMessage(ex, args);
// Custom code that sends an email to the dev team.
Email.SendUnexpectedErrorMessage("Some App", message);
}
Run Code Online (Sandbox Code Playgroud)
但是,一个"次要"问题 - 当我故意让部分代码抛出异常以便测试这种机制时......
public static void GetMuffinsByTopping(string topping)
{
throw new Exception("Test Exception!", new Exception("Test …Run Code Online (Sandbox Code Playgroud) 鉴于这个宝石的代码:
class Program
{
private static bool IsAdmin = true;
static void Main(string[] args)
{
if (!IsAdmin)
{
throw new Exception();
}
try
{
var x = 2342342;
var y = 123132;
}
catch (Exception)
{
throw;
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果this.IsAdminyield为true,我希望调试器不要输入if语句.实际上它确实 - 并且它超过了投掷,但实际上没有投掷!
现在,这只发生在if语句中有一个异常后跟一个try/catch块,在Visual Studio 2013上,针对.NET Framework 4,64位,"首选32位"未选中.
我已经与不同机器上的同事证实了这种奇怪之处.步骤虽然以下代码和调试器似乎步入if分支,但没有抛出异常:

我处于调试模式,我已经多次尝试编译和清理项目.
任何人都可以解释为什么会这样吗?
只是偶然发现了一个问题.当尝试使用Jquery检测IE 11(目前正在播出的测试版)时,结果为'firefox'.相同的代码检测IE 10.我需要知道用户正在使用什么浏览器才能显示不同的指令.
我在Oracle VirtualBox中测试是否重要.操作系统是Win 7.
这是代码:
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
var browser = function() {
if ($.browser.msie) return "ie";
var ua = navigator.userAgent.toLowerCase();
if ($.browser.mozilla/* && /firefox/.test(ua)*/) return "firefox";
if (/chrome/.test(ua)) return "chrome";
return /*"#"*/'unknown';
} ();
alert (browser); // This return firefox
alert ($.browser.version); // This returns 11.0 - the CORRECT version of IE
</script>
Run Code Online (Sandbox Code Playgroud)
如您所见,Jquery可以找到浏览器版本,但不能找到浏览器名称.知道如何绕过它吗?
我正在开发一个尝试使用此示例实现MVP模式的asp.net(经典)应用程序.在尝试对我的演示者进行单元测试并使用以下模式时,psuedocode看起来像这样
//base view interface
public interface IView
{
event EventHandler Init;
event EventHandler Load;
bool IsPostBack { get; }
void DataBind();
bool IsValid { get;}
}
//presenter psuedo code
public class SomePresenter
{
public SomePresenter(ISomeDomainService service, IView someView)
{
...
//HOW DO WE TEST/VERIFY THAT THIS REGISTRATION OCCURS?
someView.Init += OnInit;
someView.Load += OnLoad;
}
}
...
//consuming code that exercises the above code, that needs to be tested
var presenter = new SomePresenter(someDomainService, someView);
Run Code Online (Sandbox Code Playgroud)
如何验证演示者是否正在执行预期的操作,即注册Init和Load事件?虽然这很容易在Phil Haack …
我有一个控制台应用程序来测试HangFire.这是代码:
using System;
using Hangfire;
namespace MyScheduler.ConsoleApp
{
internal static class Program
{
internal static void Main(string[] args)
{
MyMethod();
Console.WriteLine("[Finished]");
Console.ReadKey();
}
private static void MyMethod()
{
RecurringJob.AddOrUpdate(() => Console.Write("Easy!"), Cron.Minutely);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但它在运行时抛出异常:
附加信息:JobStorage.Current属性值尚未初始化.您必须在使用Hangfire客户端或服务器API之前进行设置.
所以我需要一个工作存储来运行它.但是SQL存储中的所有示例等.有没有办法用某种内存存储来运行这个例子?
JobStorage.Current = new SqlServerStorage("ConnectionStringName", options);
// to
JobStorage.Current = new MemoryDbStorage(string.Empty, options);
Run Code Online (Sandbox Code Playgroud) 我的Visual Studio Ultimate 2013 Update 4无法编译ASP.NET MVC 5视图.
虽然编译总是成功的,但偶尔会在视图上发现编译错误.Intellisense也在视图上打开和关闭.我会说它在VS2012中工作得更好(我在该版本上没有太多的MVC).
我试图添加<MvcBuildViews>true</MvcBuildViews>到.csproj文件,用于在VS2010中工作的东西,但它不再工作了.
知道可能是什么问题吗?
更新:我正在寻找在视图中查看错误的方法,因为它发生在以前版本的VS中.
asp.net-mvc visual-studio razor visual-studio-2013 asp.net-mvc-5
asp.net ×6
c# ×6
.net ×4
.net-core ×2
asp.net-core ×2
asp.net-mvc ×2
hangfire ×1
jquery ×1
moq ×1
msdeploy ×1
razor ×1
unit-testing ×1
web-config ×1