小编Yur*_* N.的帖子

在 Asp.Net Core 中使用 POST 方法重定向到 URL

我有简单的 url 重写器:

    private static void RedirectToAPI(RewriteContext context)
    {
        var request = context.HttpContext.Request;
        if (request.Path.Value.StartsWith("/apiendpoint", StringComparison.OrdinalIgnoreCase))
        {           
            var json = JsonConvert.SerializeObject(request.Path.Value
                .Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries)
                .Skip(1));
            var response = context.HttpContext.Response;

            response.Headers[HeaderNames.Location] = $"/custom";
            response.StatusCode = StatusCodes.Status301MovedPermanently;
            context.Result = RuleResult.EndResponse;
            using (var bodyWriter = new StreamWriter(response.Body))
            {
                bodyWriter.Write(json);
                bodyWriter.Flush();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

问题是,当它重定向到/custom url 时,请求有方法 GET,而这个方法需要 POST。

比如发送 GET 请求到 url /apiendpoint/first/second/third,然后 rewriter 响应重定向,相应地,下面的请求必须是 POST 方法,但现在,它是 GET。

如何更改请求方法,即在 url 重写器响应之后?

c# url redirect url-rewriting asp.net-core

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

如何在Asp.Net Core 1.0.0中本地化Display属性?

我如何NameDisplay属性中进行本地化?例如:

[Display(Name = "Library name")]
public string LibraryName { get; set; }
Run Code Online (Sandbox Code Playgroud)

该属性该怎么办?

c# localization asp.net-core

5
推荐指数
1
解决办法
3512
查看次数

从 Asp.Net Core 中的 web.config 读取环境变量

这是我的web.config文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
      <environmentVariables>
        <environmentVariable name="TEST_WEBCONFIG_VARIABLE" value="some test webconfig variable value" />
      </environmentVariables>
    </aspNetCore>
  </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

如何TEST_WEBCONFIG_VARIABLE从我的web.config中读取Startup.cs

我尝试过Configuration["TEST_WEBCONFIG_VARIABLE"],但配置值列表中不存在该变量。

c# asp.net web-config asp.net-core

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

Razor 引擎在 .Net Core 类库中找不到我的视图

我有我的类库,它由ViewRenderService类组成:

public interface IViewRenderService
    {
        Task<string> RenderToStringAsync(string viewName, object model);
    }

    public class ViewRenderService : IViewRenderService
    {
        private readonly IRazorViewEngine _razorViewEngine;
        private readonly ITempDataProvider _tempDataProvider;
        private readonly IServiceProvider _serviceProvider;

        public ViewRenderService(IRazorViewEngine razorViewEngine,
            ITempDataProvider tempDataProvider,
            IServiceProvider serviceProvider)
        {
            _razorViewEngine = razorViewEngine;
            _tempDataProvider = tempDataProvider;
            _serviceProvider = serviceProvider;
        }

        public async Task<string> RenderToStringAsync(string viewName, object model)
        {
            var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider };
            var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());

            using (var sw = …
Run Code Online (Sandbox Code Playgroud)

asp.net class-library razor asp.net-core

3
推荐指数
1
解决办法
2477
查看次数

如何有效地使用Entity Framework Core?

我们来看看简单的类示例:

public class Book
{
    [Key]
    public string BookId { get; set; }
    public List<BookPage> Pages { get; set; }
    public string Text { get; set; }
} 

public class BookPage
{
    [Key]
    public string BookPageId { get; set; }
    public PageTitle PageTitle { get; set; }
    public int Number { get; set; }
}

public class PageTitle
{
    [Key]
    public string PageTitleId { get; set; }
    public string Title { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

所以,如果我想获得所有PageTitiles,如果我只知道BookId,我需要写一些包含,如下所示:

using (var dbContext = …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework entity-framework-core

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

Asp.Net Core中的SSL不起作用

我从Visual Studio Web Application(Net.Core)模板创建了具有单个用户帐户授权的简单Web应用程序。

然后,我在中启用了SSL Project->MyProject Properties...,并使用https将URL复制到了App Url。

最后,我添加了:

services.AddMvc(options =>
{
    options.Filters.Add(new RequireHttpsAttribute());
});
Run Code Online (Sandbox Code Playgroud)

Startup

而且它不起作用!当我启动该应用程序时,它只是在一秒钟后就关闭了,什么也没有发生,没有错误,什么也没有!

Asp.Net Core中的SSL有什么问题,如何正确启用它?

c# asp.net ssl asp.net-core

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