【发布时间】:2015-09-29 23:11:03
【问题描述】:
我希望网页根据之前访问的网页做不同的事情。
基本上,我知道如何制作具有持久持久性的数据。但是,我的方法涉及使用查询字符串,但我不希望第二个用户能够使用之前创建的查询字符串访问网页。因此,我试图通过将多个路由映射到单个 url 来解决这个问题。这在 C# MVC 中可行吗?
具体来说,每当我从我的视图中单击我的链接(它可以在下面找到)时,被调用的路由只是具有 URL Search_History_Page 的第一条路由。这意味着具有相应操作方法的路由不会被调用。 The_Search_History_Page
这是我的 route.config
using System.Web.Mvc;
using System.Web.Routing;
namespace CBCM_Audio_Searcher
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Route_That_Leads_To_The_Home_Page_As_The_First_Page",
url: "The_Home_Page",
defaults: new { controller = "First_", action = "Goes_To_The_Home_Page_As_The_First_Page"}
);
routes.MapRoute(
name: "Route_That_Leads_To_The_Search_History_Page_As_The_First_Page",
url: "The_Search_History_Page",
defaults: new { controller = "First_", action = "Goes_To_The_Search_History_Page_As_The_First_Page", id = 1}
);
routes.MapRoute(
name: "Route_That_Leads_To_The_Search_History_Page_From_The_Home_Page",
url: "The_Search_History_Page",
defaults: new { controller = "First_", action = "Goes_To_The_Search_History_Page_From_The_Home_Page", id=0 }
);
}
}
}
这是我的控制器。
using System.Web.Mvc;
using System.Diagnostics;
namespace CBCM_Audio_Searcher.Controllers
{
public class First_Controller : Controller
{
public ActionResult Goes_To_The_Home_Page_As_The_First_Page()
{
Database_Data_Modifier_And_Extractor Database_Data_Modifier_And_Extractor=new Database_Data_Modifier_And_Extractor();
if (Database_Data_Modifier_And_Extractor.Checks_If_A_User_Can_Access_The_Website() == false)
{
return Redirect("http://www.google.com");
}
else
{
ViewData["User_ID"]=Database_Data_Modifier_And_Extractor.User_ID;
return View("The_Home_Page");
}
}
public ActionResult Goes_To_The_Search_History_Page_As_The_First_Page(string User_ID,string DummyVariable, int id)
{
Debug.WriteLine(id);
return View("The_Search_History_Page");
}
public ActionResult Goes_To_The_Search_History_Page_From_The_Home_Page(string User_ID, string DummyVariable, int id)
{
Debug.WriteLine(id);
return View("The_Search_History_Page");
}
}
}
这是我的 The_Home_Page_View。
@Html.ActionLink("Your Search Results", "Goes_To_The_Search_History_Page_From_The_Home_Page", "First_", new { User_ID = ViewData["User_ID"], DummyVariable = "a"}, null)
我的 The_Search_History_Page 视图为空。
【问题讨论】:
-
动作选择器在这种情况下会很有帮助。
-
我认为这对我没有帮助。我希望网址相同。动作选择器改变了 url。
-
属性路由能解决您的问题吗?这个想法是您可以使用查询字符串参数作为路由条件。 blogs.msdn.com/b/webdev/archive/2013/10/17/…
标签: c# asp.net-mvc url model-view-controller routing