你好我试图建立在我的网站,现在一些物理的文章评论部分我想,当我在解析路由参数
<a asp-route-articleid="smth" asp-action="Create">Create New</a>
单击它创建了网址的网页:http://localhost:65401/Physics/Create?articleid=smth
<h2>Create</h2>
<form asp-action="Create">
<div class="form-horizontal">
<h4>Physics</h4>
<hr />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="CommentContext" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="CommentContext" class="form-control" />
<span asp-validation-for="CommentContext" class="text-danger" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
</form>
现在我的问题:我将如何去提取我的控制器中的articleid?到目前为止,我试过这个
public async Task<IActionResult> Create([Bind("ID,CommentContext,UserName,ArticleID")] Physics physics, string articleid)
{
if (ModelState.IsValid)
{
physics.UserName = HttpContext.User.Identity.Name;
physics.ArticleID = articleid;
_context.Add(physics);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(physics);
}
以及其他绝望的尝试到目前为止没有运气。任何帮助,将不胜感激。
编辑:使用HttpContext.Request.GetEncodedUrl();
让我http://localhost:65401/Physics/Create
但问题是我需要articleid=smth
值。
解决方案:
Request.Headers["Referer"].ToString();
将返回完整的URL字符串包括“条款ArticleID =不便的价值。
你确定你没有任何其他问题吗?你也确定当你点击CreateNew它会调用你上面指定的Create方法。 – dotnetstep
是的,它似乎一切工作,因为我想用我提供的解决方案,说实话,可能有更好的解决方案,我是远离有经验的程序员,但该解决方案现在做的伎俩,所以我不抱怨 – lobito