(这是一个Q/A风格的问题,旨在成为那些提出类似问题的人的首选资源.很多人似乎偶然发现了这样做的最佳方式,因为他们不知道所有的选择许多答案都是ASP.NET特有的,但AJAX和其他技术确实在其他框架中具有等价物,例如socket.io和SignalR.)
我有一个我在ASP.NET中实现的数据表.我想实时或接近实时地显示页面上此基础数据的更改.我该怎么办呢?
我的型号:
public class BoardGame
{
public int Id { get; set;}
public string Name { get; set;}
public string Description { get; set;}
public int Quantity { get; set;}
public double Price { get; set;}
public BoardGame() { }
public BoardGame(int id, string name, string description, int quantity, double price)
{
Id=id;
Name=name;
Description=description;
Quantity=quantity;
Price=price;
}
}
Run Code Online (Sandbox Code Playgroud)
代替这个例子的实际数据库,我只是将数据存储在Application变量中.我将在我Application_Start的Global.asax.cs函数中播种它.
var SeedData = new List<BoardGame>(){
new BoardGame(1, "Monopoly","Make your opponents go bankrupt!", 76, 15),
new …Run Code Online (Sandbox Code Playgroud) 我按下一个按钮启动两个线程,每个线程调用一个单独的例程,每个例程将打印线程名称和值i.
程序运行完美,但我看到Thread1()函数先运行然后Thread2()例程启动,但我尝试运行Thread1()并且Thread2()两者并行运行.我哪里弄错了?
private void button1_Click(object sender, EventArgs e)
{
Thread tid1 = new Thread(new ThreadStart(Thread1));
Thread tid2 = new Thread(new ThreadStart(Thread2));
tid1.Start();
tid2.Start();
MessageBox.Show("Done");
}
public static void Thread1()
{
for (int i = 1; i <= 10; i++)
{
Console.Write(string.Format("Thread1 {0}", i));
}
}
public static void Thread2()
{
for (int i = 1; i <= 10; i++)
{
Console.Write(string.Format("Thread2 {0}", i));
}
}
Run Code Online (Sandbox Code Playgroud) 在其中的一天中,我想从一个新项目开始,MVC似乎真的很有趣,但我想知道是否有可能将MVC 5项目升级到MVC 6项目,该项目将在今年晚些时候发布?
或者你必须重新开始,因为很多事情已经改变了?或者你建议我等到MVC 6发布?
我有一个接口,从Repository模式定义一个存储库:
interface IRepository
{
List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression);
}
Run Code Online (Sandbox Code Playgroud)
我已经针对Entity Framework实现了它:
class EntityFrameworkRepository
{
public List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression)
{
return DBContext.Customers.Where(expression).ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎运作良好,它允许我做类似的事情:
var customers = entityFrameworkRepository.Where(
customer => String.IsNullOrEmpty(customer.PhoneNumber)
);
Run Code Online (Sandbox Code Playgroud)
现在我想要一个InMemoryRepository用于测试和演示目的.我试图创建一个:
class InMemoryRepository
{
Dictionary<int, Customer> Customers {get; set;} = new Dictionary<int, Customer>();
public List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression)
{
//what do I put here?
}
}
Run Code Online (Sandbox Code Playgroud)
正如您在上面的代码中看到的那样,我对InMemoryRepository.GetAllCustomers实现的操作感到困惑.我应该在那里用提供的表达式过滤Customers并返回结果?
我试过了:
return Customers.Where(expression));
Run Code Online (Sandbox Code Playgroud)
但很明显它是期待的,Func<KeyValuePair<int, Customer>, bool>所以我得到一个编译错误:
错误CS1929'Dictionary'不包含'Where'的定义和最佳扩展方法重载'Queryable.Where(IQueryable,Expression>)'需要类型为'IQueryable'DataAccess.InMemory的接收器
我有一个ASP.NET MVC 6项目,具有以下结构:
project/
wwwroot/
custom/
project.json
Run Code Online (Sandbox Code Playgroud)
我希望提供文件,custom因为它是一个虚拟文件夹,http://localhost/custom而不必在开发过程中复制它们.
是否可以在没有IIS的虚拟文件夹的情况下在vNext中执行此操作(例如,使用StaticFile中间件)?
事实证明,从 .NET 5 升级到 .NET 6 并不像我预期的那么简单。
由于以下错误,我的所有单元测试项目都无法编译
Severity Code Description Project File Line Suppression State
Error CS0433 The type 'ServiceCollection' exists in both 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' and 'Microsoft.Extensions.DependencyInjection, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' Sample.DynamoDb.FunctionalTests C:\src\my-solution\test\Sample.DynamoDb.FunctionalTests\DependencyInjectionTests\GetServiceTests.cs 22 Active
Run Code Online (Sandbox Code Playgroud)
他们引用了依赖于版本 6 的类库,但由于某种原因,测试项目本身Microsoft.Extensions.DependencyInjection.Abstractions似乎存在依赖关系。Microsoft.Extensions.DependencyInjection可能是被我使用的一些软件包拖过去的(我不知道是哪一个)。
这是我的测试项目
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>Enable</Nullable>
<IsPackable>False</IsPackable>
<IsPublishable>False</IsPublishable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0"> …Run Code Online (Sandbox Code Playgroud) Mock<IDbContext> dbContext;
[TestFixtureSetUp]
public void SetupDbContext()
{
dbContext = new Mock<IDbContext>();
dbContext.Setup(c => c.SaveChanges()).Verifiable();
dbContext.Setup(c => c.SaveChangesAsync()).Verifiable();
dbContext.Setup(c => c.Customers.Add(It.IsAny<Customer>()))
.Returns(It.IsAny<Customer>()).Verifiable();
}
[Test]
public async Task AddCustomerAsync()
{
//Arrange
var repository = new EntityFrameworkRepository(dbContext.Object);
var customer = new Customer() { FirstName = "Larry", LastName = "Hughes" };
//Act
await repository.AddCustomerAsync(customer);
//Assert
dbContext.Verify(c => c.Customers.Add(It.IsAny<Customer>()));
dbContext.Verify(c => c.SaveChangesAsync());
}
[Test]
public void AddCustomer()
{
//Arrange
var repository = new EntityFrameworkRepository(dbContext.Object);
var customer = new Customer() { FirstName = "Larry", LastName = …Run Code Online (Sandbox Code Playgroud) 我有一个JavaScript函数,可以模糊.奇怪的是,它第一次运行它时工作正常,从那时起我就收到一个错误,说JavaScript函数未定义 - 它停止运行.我搜索过类似的问题,但没有一个建议能够解决这个问题.Asp.Net 3.5 Webforms,如果重要的话.我已经包含了一些可能与问题无关的额外函数和代码行.我遇到的问题是updateFiscalGrid,大函数.绑定到事件的HTML位于函数下方.
<%@ Page MasterPageFile="~/MasterPages/NPRPage.Master" CodeBehind="NPRFundingApplication.aspx.cs" Inherits="Tea.Hcf.Web.Secured.NPRFundingApplication" AutoEventWireup="true" Language="C#" EnableEventValidation="true" MaintainScrollPositionOnPostback="true" %>
<%@ Register TagPrefix="ew" Namespace="eWorld.UI" Assembly="eWorld.UI" %>
<%@ Register TagPrefix="hcf" Namespace="Tea.Hcf.Web" Assembly="Tea.Hcf.Web" %>
<%@ Register Src="../Controls/NpCdnSearch.ascx" TagName="NpCdnSearch" TagPrefix="np1" %>
<%@ Register Src="../Controls/NpStudentRoster.ascx" TagName="NpStudentRoster" TagPrefix="np2" %>
Run Code Online (Sandbox Code Playgroud)
<script type="text/javascript" language="javascript">
//<![CDATA[
function showMaxWin(nUrl) {
var h = 600;
var w = 800;
var features = 'resizable=1, width=' + w + ', height=' + h + ', top=0, left=0';
NewWin = window.open(nUrl, 'NewWin', features);
}
function dateChangedCallback() {
updateSubTotals(); …Run Code Online (Sandbox Code Playgroud) 当我发布我的项目并在服务器上运行它时,它工作.EPPlus找到了所有4个工作表,通过它们进行迭代,并将我的数据上传到SQL.
但是当我通过我的浏览器或我的同事浏览器运行它时,它会显示0个工作表.
知道为什么会这样吗?那时代码并不多,但是这部分是:
using (ExcelPackage package = new ExcelPackage(new FileInfo(strFile)))
{
if (package.Workbook.Worksheets.Count <= 0)
strError = "Your Excel file does not contain any work sheets";
else
{
foreach (ExcelWorksheet worksheet in package.Workbook.Worksheets)
{
Run Code Online (Sandbox Code Playgroud) 我正在使用我在C#控制台应用程序.NET Framework 4.0中通过NuGet获得的命令行解析器库.
这是我的选择课......
class Options
{
[Option('p', "prompt", DefaultValue = true, HelpText = "Prompt the user before exiting the program.")]
public bool PromptForExit { get; set; }
[HelpOption]
public string GetUsage()
{
return HelpText.AutoBuild(this, (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
}
}
Run Code Online (Sandbox Code Playgroud)
这是我解析和使用选项的地方......
static void Main(string[] args)
{
Options options = new Options();
if (CommandLine.Parser.Default.ParseArguments(args, options))
{
if (options.PromptForExit)
{
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了各种各样的命令,试图让它在退出前不提示我,但它们都没有工作.是否有人熟悉这个库或者想知道如何从命令行获取PromptForExit选项是假的?
这是我尝试过的.
myprogram.exe
myprogram.exe -p false
myprogram.exe -p False …Run Code Online (Sandbox Code Playgroud) c# ×7
asp.net ×4
.net ×2
.net-6.0 ×1
ajax ×1
asp.net-mvc ×1
async-await ×1
epplus ×1
excel ×1
func ×1
html ×1
iqueryable ×1
javascript ×1
linq ×1
moq ×1
signalr ×1
unit-testing ×1
webforms ×1