小编Kir*_*kin的帖子

如何将参数从@ Url.Action传递给控制器​​函数

我有一个功能CreatePerson(int id),我想通过id@Url.Action.

以下是参考代码:

public ActionResult CreatePerson(int id) //controller 
window.location.href = "@Url.Action("CreatePerson", "Person") + id";
Run Code Online (Sandbox Code Playgroud)

上面的代码无法将id值传递给CreatePerson函数.

c# asp.net-mvc

64
推荐指数
3
解决办法
22万
查看次数

.NET Core DI,将参数传递给构造函数的方法

拥有以下服务构造函数

public class Service : IService
{
     public Service(IOtherService service1, IAnotherOne service2, string arg)
     {

     }
}
Run Code Online (Sandbox Code Playgroud)

使用.NET Core IOC机制传递参数有哪些选择?

_serviceCollection.AddSingleton<IOtherService , OtherService>();
_serviceCollection.AddSingleton<IAnotherOne , AnotherOne>();
_serviceCollection.AddSingleton<IService>(x=>new Service( _serviceCollection.BuildServiceProvider().GetService<IOtherService>(), _serviceCollection.BuildServiceProvider().GetService<IAnotherOne >(), "" ));
Run Code Online (Sandbox Code Playgroud)

还有其他方法吗?

c# dependency-injection ioc-container .net-core asp.net-core

52
推荐指数
4
解决办法
2万
查看次数

在.NET Core 3中找不到dotnet ef

我正在关注文档以创建初始迁移。执行时dotnet,我得到了帮助部分,这意味着PATH可以正常工作。

然后,我尝试从控制台窗口的文档中执行以下命令:

dotnet ef migrations add InitialCreate  
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Could not execute because the specified command or file was not found.  
Possible reasons for this include:  
  * You misspelled a built-in dotnet command.  
  * You intended to execute a .NET Core program, but dotnet-ef does not exist.  
  * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
Run Code Online (Sandbox Code Playgroud)
  • 自从复制命令以来,我排除了第一项。
  • 由于安装了软件包Microsoft.EntityFrameworkCore.SqlServer,因此我排除了第二项。
  • 我排除了第三项,因为调用dotnet时会获得帮助部分。

我正在搜索该问题,但是由于该版本是新版本,因此没有太多工作要做,并且/或者它淹没了早期版本中的类似问题。 …

c# entity-framework-core .net-core asp.net-core .net-core-3.0

44
推荐指数
8
解决办法
1万
查看次数

asp.net core 中的 Host 和 WebHost 类有什么区别

我试图将我的应用程序从 asp.net core 2.1 迁移到 3.0,并且在 program.cs 中出现了第一个建议的更改以创建主机。

asp.net core 2.1 程序.cs

public static void Main(string[] args)
{
    CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>();
Run Code Online (Sandbox Code Playgroud)

asp.net core 3.0 程序.cs

public static void Main(string[] args)
{
    CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });
Run Code Online (Sandbox Code Playgroud)

我可以看到,在 asp.net core 3.0 中,它正在创建 Host 而不是 WebHost 并在创建 Host 时注入 WebHostBuilder。

但是我在这里没有明确的想法 Host 和 WebHost 之间有什么区别以及为什么 asp.net core 3.0 应用程序不允许创建 WebHost?

c# asp.net-core asp.net-core-2.1 asp.net-core-3.0

41
推荐指数
1
解决办法
8017
查看次数

在ASP.NET核心中注入IUrlHelper

RC1中,IUrlHelper可以注入服务(services.AddMvc()在启动类中)

这在RC2中不再起作用.有没有人知道如何在RC2中做到这一点,因为刚刚UrlHelper需要一个ActionContext对象.不知道如何在控制器之外得到它.

c# asp.net-core-mvc asp.net-core asp.net-core-1.0

28
推荐指数
4
解决办法
8672
查看次数

相当于.net Core webapi 2的HttpResponseException/IHttpActionResponse(不是mvc)

当我阅读关于webapi以响应请求和处理错误时,一切都基于:

IHttpActionResult
HttpResponseException
Run Code Online (Sandbox Code Playgroud)

但是当您创建.net核心webapi项目时,这些项目不可用.我能找到IActionResult,它似乎相当于?

但是我要绕圈试图找出一个非常简单的东西,即如何处理.net核心webapi中的错误,因为HttpResponseException不可用.我得到的印象是"http"中的所有内容,仅适用于完整的MVC应用程序.

我想做的就是返回一个错误,当然它必须简单......

c# asp.net-core asp.net-core-webapi

24
推荐指数
1
解决办法
1万
查看次数

Angular 6:ERROR TypeError:"......不是函数" - 但确实如此

我现在真的很困惑,因为我得到了ERROR TypeError: "_this.device.addKeysToObj is not a function".但我实现了这个功能,所以我不知道问题是什么或为什么它不可调用.我已经尝试使用Firefox和chrome的代码,两者都是通过相同的错误.

错误符合 this.device.addKeysToObj(this.result.results[0]);

这是我的班级:

export class Device {
    id: number;
    deviceID: string;
    name: string;
    location: string;
    deviceType: string;
    subType: string;
    valueNamingMap: Object;

    addKeysToObj(deviceValues: object): void {
        for (let key of Object.keys(deviceValues).map((key) => { return key })) {
            if (!this.valueNamingMap.hasOwnProperty(key)) {
                this.valueNamingMap[key] = '';
            }
        }
        console.log(this, deviceValues);
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是电话:

export class BatterieSensorComponent implements OnInit {
    @Input() device: Device;
    public result: Page<Value> = new Page<Value>();

    //[..]

    ngOnInit() {
      this.valueService.list('', this.device).subscribe(
        res => …
Run Code Online (Sandbox Code Playgroud)

javascript typescript angular

22
推荐指数
1
解决办法
2万
查看次数

使用Karma,如何排除与特定子文件夹中的模式匹配的所有文件?

我有一个简单的AngularJs应用程序设置,看起来有点像这样:

client
    vendor
        angular
        bootstrap
        jquery
        ...
    app.module.js
    app.controller.js
    ...
node_modules
    angular-mocks
    ...
Run Code Online (Sandbox Code Playgroud)

我正在设置一个karma.conf.js文件,我想要包含angular\angular.js来自vendor.我不想包含任何其他内容vendor.

以下是我在相关karma.conf.js部分的内容:

files: [
    'client/vendor/angular/angular.js',
    'client/**/*.js',
    'client/**/*.spec.js'
],
exclude: [
    'client/vendor/**/*.js'
],
Run Code Online (Sandbox Code Playgroud)

我遇到的问题很简单:我angular.js明确地包括在内files,但它在exclude部分模式中被排除在外.

我怎样才能排除client/vendorangular/angular.js(以后可能是其他人)以外的所有内容?该client目录包含许多文件,子文件夹等,其中包含我自己的.js文件,因此仅将我想要包含的所有内容移动到自己的文件夹中并不容易.

javascript angularjs karma-jasmine

20
推荐指数
1
解决办法
2万
查看次数

在ASP.NET Core Identity UI中更改路由?

自ASP.NET Core 2.1发布以来,我正在使用新的Identity UI软件包.使用新生成的MVC项目,这里有一些可用的页面URL:

/Home/About
/Home/Contact
/Identity/Account/Login
/Identity/Account/Register
Run Code Online (Sandbox Code Playgroud)

如何配置路由以/Identity/从URL中删除部分?

c# asp.net-core asp.net-core-identity

18
推荐指数
3
解决办法
6406
查看次数

HTTPClient POST尝试解析非JSON响应

我正在尝试在Angular中发出请求,我知道HTTP响应不是JSON而是文本.但是,Angular似乎期待JSON响应,因为错误如下:

SyntaxError:XMLHttpRequest.c中JSON.parse()位置0的JSON中的意外标记<

以及

解析http:// localhost:9时出现 Http失败...

这是post方法:

return this.http.post(this.loginUrl, this.createLoginFormData(username, password), this.httpOptions)
  .pipe(
    tap( // Log the result or error
      data => console.log(data);
      error => console.log(error)
    )
  );
Run Code Online (Sandbox Code Playgroud)

和标题.

private httpOptions = {

  headers: new HttpHeaders({
    'Accept': 'text/html, application/xhtml+xml, */*',
    'Content-Type': 'application/x-www-form-urlencoded',
    responseType: 'text'
  },

) };
Run Code Online (Sandbox Code Playgroud)

我认为这responseType: 'text'足以让Angular期望非JSON响应.

typescript angular angular-httpclient

18
推荐指数
4
解决办法
1万
查看次数