開發 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 關注
- 183 瀏覽
添加回答
舉報
0/150
提交
取消