出于某些奇怪的原因,我想从控制器操作直接将HTML写入Response流.(我理解MVC分离,但这是一个特例.)
我可以直接写入HttpResponse
流吗?在这种情况下,IView
控制器操作应该返回哪个对象?我可以退回'null'吗?
我有以下linq-to-entities查询与2个连接表,我想添加分页:
IQueryable<ProductInventory> data = from inventory in objContext.ProductInventory
join variant in objContext.Variants
on inventory.VariantId equals variant.id
where inventory.ProductId == productId
where inventory.StoreId == storeId
orderby variant.SortOrder
select inventory;
Run Code Online (Sandbox Code Playgroud)
我意识到我需要使用.Join()扩展方法,然后调用.OrderBy().跳过().Take()执行此操作,我只是绊倒了Join()的语法,似乎无法找到任何例子(在线或书籍).
注意:我加入表格的原因是进行排序.如果有更好的方法根据相关表中的值而不是连接进行排序,请将其包含在您的答案中.
2可能的解决方案
我想这只是一个可读性的问题,但这两个都可以工作,并且在语义上是相同的.
IQueryable<ProductInventory> data = objContext.ProductInventory
.Where(y => y.ProductId == productId)
.Where(y => y.StoreId == storeId)
.Join(objContext.Variants,
pi => pi.VariantId,
v => v.id,
(pi, v) => new { Inventory = pi, Variant = v })
.OrderBy(y => y.Variant.SortOrder)
.Skip(skip)
.Take(take)
.Select(x => x.Inventory);
Run Code Online (Sandbox Code Playgroud)
var query = from …
Run Code Online (Sandbox Code Playgroud) 在MVC-5中,我可以routetable
通过访问来编辑初始启动之后RouteTable.Routes
.我希望在MVC-6中也这样做,这样我就可以在运行时添加/删除路由(对CMS很有用).
在MVC-5中执行此操作的代码是:
using (RouteTable.Routes.GetWriteLock())
{
RouteTable.Routes.Clear();
RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
RouteTable.Routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Run Code Online (Sandbox Code Playgroud)
但我RouteTable.Routes
在MVC-6中找不到或类似的东西.知道如何在运行时更改路径集合吗?
我想在CMS中创建页面时使用此原则添加额外的URL.
如果你有一个类:
public class Page
{
public int Id { get; set; }
public string Url { get; set; }
public string Html { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和控制器像:
public class CmsController : Controller
{
public ActionResult Index(int id)
{
var page = DbContext.Pages.Single(p => p.Id …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将此示例RouteBase实现转换为与MVC 6一起使用.我已经通过遵循路由项目中的示例来解决大部分问题,但是我正在了解如何Task
从方法返回异步.我真的不在乎它是否实际上是异步的(为任何可以提供答案的人欢呼),现在我只想让它运转起来.
我有传出的路线功能(ActionLink
当我输入路线值时,意味着工作正常).问题在于RouteAsync
方法.
public Task RouteAsync(RouteContext context)
{
var requestPath = context.HttpContext.Request.Path.Value;
if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
{
// Trim the leading slash
requestPath = requestPath.Substring(1);
}
// Get the page that matches.
var page = GetPageList()
.Where(x => x.VirtualPath.Equals(requestPath))
.FirstOrDefault();
// If we got back a null value set, that means the URI did not match
if (page != null)
{
var routeData = new RouteData();
// This …
Run Code Online (Sandbox Code Playgroud) c# asp.net-mvc asp.net-mvc-routing asp.net-core-mvc asp.net-core
在.NET框架中,您可以创建一个.EXE
将从命令行运行的单个文件,而无需任何额外的配置文件(如果使用ILMerge,则可以将所有.DLL
引用放入1个.EXE
程序集中).
我正在尝试使用.NET Core来完成同样的事情,但到目前为止还没有成功.即使是Hello World
没有依赖关系的最简单的应用程序也需要有一个命名的文件<MyApp>.runtimeconfig.json
才能运行dotnet.exe
.
dotnet F:\temp\MyApp.dll
Run Code Online (Sandbox Code Playgroud)
内容<MyApp>.runtimeconfig.json
如下:
{
"runtimeOptions": {
"framework": {
"name": "Microsoft.NETCore.App",
"version": "1.1.1"
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果没有与此文件夹相同的配置文件.DLL
,我会收到以下错误:
A fatal error was encountered. The library 'hostpolicy.dll' required to
execute the application was not found in 'F:\temp'.
Run Code Online (Sandbox Code Playgroud)
我的问题是:有没有办法改变应用程序,所以它不需要这个配置文件存在,所以这些信息的默认值是在编译内,.DLL
但可以通过添加配置文件来覆盖?
注意:如果平台具有正确版本的.NET Core,我还要确保它"正常工作"而不管它上面安装的平台.
我试图获得一个流畅的用户体验,运行一些有时很有用的实用程序,但很少需要.因为它似乎没有可以使用相同的.DLL
是从客户端应用程序中引用作为控制台应用程序,未来最好的事情是有一个单一的文件是可以下载,没有任何依赖性运行.
例如,在Java中,您只需.jar
在任何支持的平台上下载文件并运行:
java <package>.jar <namespace>.SomeClass [args]
Run Code Online (Sandbox Code Playgroud)
并且它将"正常工作"而没有任何额外的文件.如何使用.NET Core获得类似的用户体验?
简而言之,我想尽量避免"首先解压缩到目录"的额外步骤......
.net c# console-application command-line-interface .net-core
有没有办法获得IServiceProvider.GetService<T>
返回实例,即使T
没有明确注册容器?
如果我知道T
有依赖关系,我希望根据他们的注册注入它们而不必注册T
自己.
我相信如果没有找到合适的构造函数,Ninject将智能地计算出最合适的构造函数或回退到无参数构造函数.如果可能的话,我想使用标准的MVC Core DI框架来复制这种行为.
尝试在刚刚创建的新解决方案中启用包还原时出现错误.VS2012中的错误是:
NuGet包管理器
配置解决方案以在构建时还原NuGet包时发生错误
无法从路径'NuGet.Build.2.7.0.npkg'读取包.
我尝试在VS2010中打开解决方案来解决问题,我在尝试启用包恢复时也遇到错误,但消息不同:
NuGet包管理器
配置解决方案以在构建时还原NuGet包时发生错误
存档文件的大小不能为0.
我尝试创建一个新的解决方案,但得到了相同的结果.
然后我尝试在VS2012更新3上进行修复并重新启动.仍然遇到问题.
我还扫描了文件夹,项目和解决方案文件中的任何NuGet或.nupkg,但没有任何内容.
那么我怎样才能让这个功能再次运行?我最后一次使用它是在大约一个星期前,我不记得我从那时起改变了什么.我卸载了大约一周前安装的VS Power Tools软件包,但这也没有解决问题.
更新
我在这里按照"删除"说明并使用我已经拥有的项目作为模板来手动启用包恢复.但是,我仍在寻找更好的解决方案,因为这是我经常使用的功能.
我也尝试从visual studio卸载并重新安装NuGet,但我仍然遇到同样的问题.如果内存服务正常,最近有一个NuGet更新(是否有VS扩展安装的日志,所以我可以检查?).
visual-studio-2010 visual-studio nuget visual-studio-2012 nuget-package-restore
我参与了一个目前支持MVC 2 - MVC 5的开源库,我也想支持MVC 6(以及更高版本).为了支持每个版本的MVC,我们利用MSBuild 的Condition特性在构建时包含正确版本的MVC及其依赖项(取决于其值DefineConstants
).通过使用相同的项目文件和源代码为每个MVC版本创建单独的DLL,可以为所有受支持的MVC版本使用单个项目文件.
<ItemGroup Condition=" $(DefineConstants.Contains('MVC2')) ">
<Reference Include="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
</ItemGroup>
<ItemGroup Condition=" $(DefineConstants.Contains('MVC3')) ">
<!-- Due to the windows update MS14-059, we need this hack to ensure we can build MVC3 both on machines that have the update and those that don't -->
<Reference Condition=" Exists('$(windir)\Microsoft.NET\assembly\GAC_MSIL\System.Web.Mvc\v4.0_3.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll') " Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Condition=" !Exists('$(windir)\Microsoft.NET\assembly\GAC_MSIL\System.Web.Mvc\v4.0_3.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll') " Include="System.Web.Mvc, Version=3.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.Mvc.3.0.20105.1\lib\net40\System.Web.Mvc.dll</HintPath>
</Reference>
<Reference Include="System.Web.Razor, …
Run Code Online (Sandbox Code Playgroud) 我正在移植.NET 3.5 - 4.5中的一些代码.在我的程序集内部,我有一些代码从当前正在执行的程序集中读取资源.但是,DNX核心5.0中GetExecutingAssembly()
的Assembly
类型不是方法.
var xsdStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(xsdPath);
Run Code Online (Sandbox Code Playgroud)
Assembly.GetExecutingAssembly()
在DNX核心5.0中相当于什么?或者,如果我需要一个命名空间来获取该方法(也许是扩展方法?),那么命名空间是什么?
我正在创建帮助程序类,以简化IServiceCollection
库的接口配置和注入.库构造函数包含许多可能先前已注入的依赖项.如果它们尚未插入IServiceCollection
,则辅助类应添加它们.如何检测接口是否已注入?
public static void AddClassLibrary(this IServiceCollection services
, IConfiguration configuration)
{
//Constructor for ClassLibrary requires LibraryConfig and IClass2 to be in place
//TODO: check IServiceCollection to see if IClass2 is already in the collection.
//if not, add call helper class to add IClass2 to collection.
//How do I check to see if IClass2 is already in the collection?
services.ConfigurePOCO<LibraryConfig>(configuration.GetSection("ConfigSection"));
services.AddScoped<IClass1, ClassLibrary>();
}
Run Code Online (Sandbox Code Playgroud) c# ×7
asp.net-core ×4
asp.net-mvc ×4
.net ×2
.net-core ×2
dnx ×1
join ×1
linq ×1
nuget ×1
pagination ×1
roslyn ×1
routes ×1