Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp.Net:从QueryString在Razor cshtml中绑定模型属性值_C#_Asp.net_Asp.net Mvc_Asp.net Core_Razor Pages - Fatal编程技术网

C# Asp.Net:从QueryString在Razor cshtml中绑定模型属性值

C# Asp.Net:从QueryString在Razor cshtml中绑定模型属性值,c#,asp.net,asp.net-mvc,asp.net-core,razor-pages,C#,Asp.net,Asp.net Mvc,Asp.net Core,Razor Pages,我试图从url查询参数绑定模型类属性值,但它似乎不起作用 浏览器将url从App1重定向到app2: public async Task<IActionResult> ApplicationSwitcher() { var redirectUrl="http://localhost:9040/app2/Home/UserNameAuthentication?tenantName=testtenant"; return Redirect(redirec

我试图从url查询参数绑定模型类属性值,但它似乎不起作用

浏览器将url从App1重定向到app2:

public async Task<IActionResult> ApplicationSwitcher()
{
  var redirectUrl="http://localhost:9040/app2/Home/UserNameAuthentication?tenantName=testtenant";   
  return Redirect(redirectUrl);     
}
public class LoginModel
{
  public string tenantName {get;set;}
}
public IActionResult UsernameAuthentication()
{
  //retrived redirection request and open current viewpage.
  return View();
}

    [HttpGet]
    public async Task<IActionResult> UsernameAuthentication(LoginModel loginModel)
    {
      Console.WriteLine(loginModel.tenantName);//null
    }
http://localhost:9040/app2/Home/UserNameAuthentication?tenantName=testtenant

应用程序1:

public async Task<IActionResult> ApplicationSwitcher()
{
  var redirectUrl="http://localhost:9040/app2/Home/UserNameAuthentication?tenantName=testtenant";   
  return Redirect(redirectUrl);     
}
public class LoginModel
{
  public string tenantName {get;set;}
}
public IActionResult UsernameAuthentication()
{
  //retrived redirection request and open current viewpage.
  return View();
}

    [HttpGet]
    public async Task<IActionResult> UsernameAuthentication(LoginModel loginModel)
    {
      Console.WriteLine(loginModel.tenantName);//null
    }
UsernameAuthenticationViewPage cshtml

<div class="inner-box">

        @using ICommonInterfaces.Model

        @model LoginModel
        @using (Html.BeginForm("UsernameAuthentication", "Home", FormMethod.Get))
        {
          @if (ViewContext.HttpContext.Request.Query.Keys.Count > 0 && !string.IsNullOrEmpty(ViewContext.HttpContext.Request.Query["tenantName"]))
          {
            @Html.HiddenFor(m => m.tenantName, ViewContext.HttpContext.Request.Query["tenantName"].ToString())
          }
          else
          {
            @Html.HiddenFor(m => m.tenantName)
          }
          <input type="submit" value="NEXT" class="rectangle" id="submitNext" disabled="disabled" />
}
</div>

@使用ICommonInterfaces.Model
@模型逻辑模型
@使用(Html.BeginForm(“UsernameAuthentication”、“Home”、FormMethod.Get))
{
@if(ViewContext.HttpContext.Request.Query.Keys.Count>0&&!string.IsNullOrEmpty(ViewContext.HttpContext.Request.Query[“tenantName”]))
{
@Html.HiddenFor(m=>m.tenantName,ViewContext.HttpContext.Request.Query[“tenantName”].ToString())
}
其他的
{
@Html.HiddenFor(m=>m.tenantName)
}
}
家庭控制器:

public async Task<IActionResult> ApplicationSwitcher()
{
  var redirectUrl="http://localhost:9040/app2/Home/UserNameAuthentication?tenantName=testtenant";   
  return Redirect(redirectUrl);     
}
public class LoginModel
{
  public string tenantName {get;set;}
}
public IActionResult UsernameAuthentication()
{
  //retrived redirection request and open current viewpage.
  return View();
}

    [HttpGet]
    public async Task<IActionResult> UsernameAuthentication(LoginModel loginModel)
    {
      Console.WriteLine(loginModel.tenantName);//null
    }
public IActionResult UsernameAuthentication()
{
//检索重定向请求并打开当前视图页。
返回视图();
}
[HttpGet]
公共异步任务用户名身份验证(LoginModel LoginModel)
{
Console.WriteLine(loginModel.tenantName);//null
}
我得到空值的原因是什么?如何从查询或查询字符串url中提取值并设置为loginModel属性(tenantName)

谢谢。

这里是一个演示(app1=>controller action=>cshtml=>controller action):

app2:HomeController(本地主机:44358):

app2:Startup.cs:

readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            services.AddCors(options =>
            {
                options.AddPolicy(name: MyAllowSpecificOrigins,
                                  builder =>
                                  {
                                      builder.AllowAnyOrigin();
                                  });
            });
           
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
           
           
            
            app.UseHttpsRedirection();
            
            app.UseStaticFiles();

            app.UseRouting();
            app.UseCors(MyAllowSpecificOrigins);
            app.UseAuthorization();
app1:Controller(localhost:44389),我在项目中使用https,重定向到app1:

public async Task<IActionResult> ApplicationSwitcher()
        {
            var redirectUrl = "https://localhost:44358/Home/UserNameAuthentication?tenantName=testtenant";
            return Redirect(redirectUrl);
        }
public异步任务ApplicationSwitcher()
{
var重定向URL=”https://localhost:44358/Home/UserNameAuthentication?tenantName=testtenant";
返回重定向(重定向URL);
}
cshtml(我将其更改为post,并更改ActionName): @模型逻辑模型

<div class="inner-box">
    @using (Html.BeginForm("UsernameAuthenticationPost", "Home", FormMethod.Post))
    {
        @if (ViewContext.HttpContext.Request.Query.Keys.Count > 0 && !string.IsNullOrEmpty(ViewContext.HttpContext.Request.Query["tenantName"]))
        {
            @Html.HiddenFor(m => m.tenantName, ViewContext.HttpContext.Request.Query["tenantName"].ToString())
        }
        else
        {
            @Html.HiddenFor(m => m.tenantName)
        }
        <input type="submit" value="NEXT" class="rectangle" id="submitNext"/>
    }
</div>

@使用(Html.BeginForm(“UsernameAuthenticationPost”,“Home”,FormMethod.Post))
{
@if(ViewContext.HttpContext.Request.Query.Keys.Count>0&&!string.IsNullOrEmpty(ViewContext.HttpContext.Request.Query[“tenantName”]))
{
@Html.HiddenFor(m=>m.tenantName,ViewContext.HttpContext.Request.Query[“tenantName”].ToString())
}
其他的
{
@Html.HiddenFor(m=>m.tenantName)
}
}
结果:

它不使用[FromQuery]也…
公共异步任务用户名验证([FromQuery]LoginModel LoginModel)
…我在HttpContext.Request.Path中看到的bczchanged@r08您如何测试
[FromQuery]
?如果您使用的是表单,您应该使用
[FromForm]
。当我复制您的代码时,它似乎对我有效。相同的代码相同的实现我正在尝试从另一个应用程序重定向到上面提到的url应用程序…它被重定向并打开了我在上面的示例中提供的cshtml页面…现在在sumbit上,我希望HomeControler方法
公共异步任务UsernameAuthentication(LoginModel LoginModel){
…但它不起作用。@HMZ我在方法参数前面提供了
FromQuery
FromForm
。但两者都不起作用…请提供详细信息上下文做得好,我本想复制,但一直很忙。感谢您宝贵的时间和努力…+v1…但我仍然没有在您的示例中了解如何以及从何处进行
[HttpGet……用户名验证(LoginModel LoginModel…
值绑定…我在redirecturl中看到,
/app/Home/…
app name缺少它不是这样的,因为它绑定的是同一个应用程序…我很容易更新现有的应用程序代码。在示例中,有两个应用程序,公共异步任务ApplicationSwitcher()=>重定向到[HttpGet]public IActionResult UsernameAuthentication(LoginModel LoginModel),在包含HomeController的项目中,我向其中添加了cors。我已更新了我的答案,以共享有关cors的更多信息。