[英]Asp.net view not call controller method
我有视图首页
这是它的代码
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
</div>
<div style="width: 100%; margin-top: 60px;">
<div style="width: 65%; margin-left: auto; margin-right: auto;">
<form id="form" style="text-align: center;" asp-action="ShortenUrl" method="post">
<input
type="text"
placeholder="Enter Url ..."
style="width: 100%; border-radius: 5px; height: 45px;"
name="longUrl"/>
<button
style="background-color: darkgreen; color: white; padding: 10px; margin-top: 25px; border-radius: 8px;"
type="submit">
Shorten Url
</button>
</form>
</div>
</div>
这是我的 controller
public class HomeController : Controller
{
private readonly IShortLinkAppService _shortLinkAppService;
public HomeController(IShortLinkAppService shortLinkAppService)
{
_shortLinkAppService = shortLinkAppService;
}
public IActionResult Index()
{
return View();
}
[HttpGet]
public async Task<IActionResult> Index(string input)
{
var shortLink = await _shortLinkAppService.Redirect(input);
if (shortLink == null)
{
return View();
}
return Redirect(shortLink.OriginalUrl);
}
[HttpPost]
public async Task<IActionResult> ShortenUrl(string longUrl)
{
var shortLink = await _shortLinkAppService.GenerateShortUrl(longUrl);
return View(shortLink);
}
}
当我单击按钮提交时,没有任何反应,断点在var shortLink = await _shortLinkAppService.GenerateShortUrl(longUrl);
不打
这是启动配置
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
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.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection"),
b => b.MigrationsAssembly("UrlShortenerApi.EntityFrameworkCore")));
//Mapper
Mapper.Initialize(cfg => cfg.AddProfile<MappingsProfile>());
services.AddAutoMapper(typeof(Startup));
services.Scan(scan =>
scan.FromAssemblyOf<IShortLinkAppService>()
.AddClasses()
.AsMatchingInterface());
services.AddControllersWithViews();
}
// 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.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
我该如何解决这个问题?
尝试用 Html.BeginForm 替换表格并添加 model
@model ViewModel
@using (Html.BeginForm("ShortenUrl", "Home", FormMethod.Post) {
<input asp-for="LongUrl" class="form-control" style="text-align: center;" />
}
由于您使用的是 mvc,因此您也需要一个 model
public class ViewModel
{
public string LongUrl get; set;
}
和行动
public IActionResult Index()
{
var model = new ViewModel();
return View(model);
}
public async Task<IActionResult> ShortenUrl(ViewModel model)
{
var longUrl=model.LongUrl;
....your code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.