获取HTML文本字段并在c#(cshtml)中用作变量

获取HTML文本字段并在c#(cshtml)中用作变量,c#,html,razor,C#,Html,Razor,我有这个HTML输入 <input type="text" placeholder="Username" name="usernameField" id="username" runat="server"> 这是完整的c#cshtml方法: @using System; @using Npgsql; @using HtmlAgilityPack; @functions{ public Boolean ValidateLogin() {

我有这个HTML输入

<input type="text" placeholder="Username" name="usernameField" id="username" runat="server">
这是完整的
c#cshtml
方法:

@using System;
@using Npgsql;
@using HtmlAgilityPack;
@functions{

    public Boolean ValidateLogin()
    {        

        NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;User Id=postgres;
Password=postgres;Database=login;");
        // Open Database connection.
        conn.Open();

        // Define a query returning a single row result set.
        NpgsqlCommand command = new NpgsqlCommand("SELECT username, password FROM logindata 
WHERE username = " + "'" + username + "'" + " AND password = " + "'" + password + "'" + ";", conn);

        // Execute the query and obtain a result set.
        NpgsqlDataReader dr = command.ExecuteReader();

        // If result exists return true and gain access to the control website.
        if (dr.Read())
        {
            return true;
        }

        // Close Database connection.
        conn.Close();
        return false;
    }
}
更多信息:

  • 我正在visual studio 2017中进行此操作
  • 我正在使用asp.net内核
  • 我正在使用cshtml文件(我相信也称为razor)

您在这里使用的技术称为Razor Pages,这是ASP.NET核心MVC的一项新功能,它使编写以页面为中心的场景更容易、更高效。()

如果要查找有关如何使用模型-视图-控制器方法的信息,请选中

对于您的问题,您需要一个
Login.cshtml
和一个
Login.cshtml.cs
文件,如果您右键单击
pages
文件夹并选择
Add
Razor Page…
,该文件将自动为您创建

这些文件应包含:

Login.cshtml

@page
@model WebApplication1.Pages.LoginModel

<form method="POST">
    <input type="text" placeholder="Username" asp-for="UserName">
    <input type="password" asp-for="Password">

    <input type="submit" />
</form>

请注意,我更改了您在查询中使用
用户名
密码
的方式,以防止SQL注入攻击。

文本字段是否有ASP.NET名称?它在服务器端运行吗?没有asp名称,它只是HTML中的一个普通文本字段,它在客户端运行。感谢您的帮助,我已经尝试了您的代码,并且没有收到任何错误,如何测试它是否工作?我可以返回一个警报或什么来看看它是否有效吗?OnPost方法是否应该在某个地方被调用,或者我是否应该在“onclick”@daddybig之类的按钮上放置一些东西?不,你不需要
onclick
。单击提交按钮后,
OnPost
将自动执行并设置
IsloggedIn
值。您可以在
cshtml
文件中使用它,检查用户是否已经登录,或者您可以将其重定向到另一个页面。我建议您阅读一下文档,以便更熟悉这些内容。@daddybig类似用户身份验证的内容已经在ASP MVC中实现,您可以轻松使用它们。我在Post return RedirectToPage(“)上创建了此方法;但当我单击submit按钮时,页面会更新,但什么也没有发生
@page
@model WebApplication1.Pages.LoginModel

<form method="POST">
    <input type="text" placeholder="Username" asp-for="UserName">
    <input type="password" asp-for="Password">

    <input type="submit" />
</form>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace WebApplication1.Pages
{
    public class LoginModel : PageModel
    {
        [BindProperty]
        public string UserName { get; set; }

        [BindProperty]
        public string Password { get; set; }

        public bool IsloggedIn { get; private set; }

        public void OnPost()
        {
            NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;User Id=postgres; Password = postgres; Database = login; ");
            // Open Database connection.
            conn.Open();

            // Define a query returning a single row result set.
            NpgsqlCommand command = new NpgsqlCommand("SELECT username, password FROM logindata WHERE username = @username AND password = @password", conn);

            command.Parameters.AddWithValue("@username", UserName);
            command.Parameters.AddWithValue("@password", Password);

            // Execute the query and obtain a result set.
            NpgsqlDataReader dr = command.ExecuteReader();

            // If result exists return true and gain access to the control website.
            if (dr.Read())
            {
                IsloggedIn = true;
            }

            // Close Database connection.
            conn.Close();
        }
    }
}