开发 url-shorter 用于自我教育,但未能实现基本功能 - 2 个工作视图。尝试了不同的动作重载Redir(),Redir(string hash)。还有这么多不同的路由版本我在控制器中得到了该操作[HttpGet]public IActionResult Index(){ return View(null);}[HttpPost]public IActionResult Index(string url,[FromServices] IComputeHash computeService){ ... return View("Index", url_builder);//url_builder is link like "localhost:44397/Home/Redir?hash=D9F57F9E10FB7CB61F178582A9DD6C1A"}public IActionResult Redir() // I also tried version with parameter Redir(string hash){ string hash = Request.Query.FirstOrDefault(p => p.Key == "hash").Value; DbField field = db.UrlHashes.Find(hash);// db {key=hash, value=link} if (field != null) { return View("Redir"); }//View is used for testing is action working at all or not //{ return Redirect(field.link); }//actually it just need to redirect to a link else { return RedirectToAction("Error"); }}public IActionResult Error(){ ... }目前我使用这种路由设置,并尝试了我想到的一切app.UseMvc(routes => { routes.MapRoute( name: "redir", template: "{controller=Home}/{Redir}/{hash}"); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/"); });当我要访问localhost:44397/Home/Redir?hash=D9F57F9E10FB7CB61F178582A9DD6C1A类型链接时,我刚刚遇到浏览器错误,没有加载视图,没有重定向到错误页面,甚至没有因某些错误而崩溃。只是普通浏览器 - 我无法加载这个东西。
2 回答

30秒到达战场
TA贡献1828条经验 获得超6个赞
您的路线配置出现问题,使用它可以解决您的问题。实际上你的哈希参数是你的网址的一部分,但你期望在查询字符串中,所以你需要改变你的路线..
app.UseMvc(routes =>
{
routes.MapRoute(
name: "redir",
template: "{controller=Home}/{Redir}");
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/");
});

红糖糍粑
TA贡献1815条经验 获得超6个赞
我发现了no-referrer-when-downgrade
错误 - 这意味着我在没有注意到浏览器的情况下来到了https
这个http
级别。所以我手动添加https://
到我的 url 构造函数中。如果有人知道如何减少硬编码,请随意发表评论。
- 2 回答
- 0 关注
- 136 浏览
添加回答
举报
0/150
提交
取消