在 asp.net mvc 4 中更改 URL 路由
change URL of route in asp.net mvc 4
在我的应用程序中,我在一个视图中创建记录并在另一个视图中显示它。
下面是控制器动作
[HttpPost]
public ActionResult Create(Domain.Entities.Survey survey)
{
ISurveyRepository surveyRepository = new DbSurveyRepository();
surveyRepository.CreateSurvey(survey);
TempData.Add("surveyID",survey.ID);
return RedirectToAction("SingleSurvey");
}
public ActionResult SingleSurvey()
{
if (TempData["surveyID"] != null)
{
ISurveyRepository surveyRepository = new DbSurveyRepository();
Domain.Entities.Survey survey = surveyRepository.GetBySurveyID((int) TempData["surveyID"]);
return View(survey);
}
return View();
}
有两种观点
1. 创建
2. 单一调查
现在,当我 return 通过操作 "SingleSurvey" 查看 "SingleSurvey" 时,浏览器上显示的 URL 是 http://localhost:49611/SingleSurvey
。
但是我想改变这个URL。我要的是http://localhost:49611/SingleSurvey/{my record id}/{my record title}
有什么办法吗?
向 get 方法添加 2 个可为 null 的参数
public ActionResult SingleSurvey(int? id, string title)
{
// if id is not null, get the survey based on id
return View(survey);
}
然后在post方法中,使用参数重定向
[HttpPost]
public ActionResult Create(Domain.Entities.Survey survey)
{
ISurveyRepository surveyRepository = new DbSurveyRepository();
surveyRepository.CreateSurvey(survey);
return RedirectToAction("SingleSurvey", new { id= survey.ID, title = survey.Title });
}
您可能还想定义一条路线,以便 url 是 .../SingleSurvey/someID/someTitle
而不是 .../SingleSurvey?id=someID&title=someTitle
旁注:在创建新调查的情况下,初始化 Survey
的新实例并使用 return View(survey);
而不是 return View()
性能更好
在您的路由配置文件中添加以下路由:
routes.MapRoute(
"SingleSurvey",
"{controller}/{action}/{id}/{title}",
new { controller = "Survey", action = "SingleSurvey", id = UrlParameter.Optional, title = UrlParameter.Optional }
);
然后更新创建操作以将 ID 和标题作为路由值的一部分传递:
[HttpPost]
public ActionResult Create(Domain.Entities.Survey survey)
{
ISurveyRepository surveyRepository = new DbSurveyRepository();
surveyRepository.CreateSurvey(survey);
TempData.Add("surveyID",survey.ID);
return RedirectToAction("SingleSurvey", new { id = survey.Id, title = survey.Title );
}
此外,与其使用 TempData 传递 ID,不如简单地从 URL 中读取 ID。为此,请更新 SingleSurvey
操作以将 ID 作为参数:
public ActionResult SingleSurvey(int? id)
{
if (id != null)
{
ISurveyRepository surveyRepository = new DbSurveyRepository();
Domain.Entities.Survey survey = surveyRepository.GetBySurveyID(id.Value);
return View(survey);
}
return View();
}
MVC框架自动绑定路由中定义的id
参数到action方法的id
参数。如果您需要在 SingleSurvey 操作中使用标题,您还可以将其添加为额外参数。
在我的应用程序中,我在一个视图中创建记录并在另一个视图中显示它。 下面是控制器动作
[HttpPost]
public ActionResult Create(Domain.Entities.Survey survey)
{
ISurveyRepository surveyRepository = new DbSurveyRepository();
surveyRepository.CreateSurvey(survey);
TempData.Add("surveyID",survey.ID);
return RedirectToAction("SingleSurvey");
}
public ActionResult SingleSurvey()
{
if (TempData["surveyID"] != null)
{
ISurveyRepository surveyRepository = new DbSurveyRepository();
Domain.Entities.Survey survey = surveyRepository.GetBySurveyID((int) TempData["surveyID"]);
return View(survey);
}
return View();
}
有两种观点 1. 创建 2. 单一调查
现在,当我 return 通过操作 "SingleSurvey" 查看 "SingleSurvey" 时,浏览器上显示的 URL 是 http://localhost:49611/SingleSurvey
。
但是我想改变这个URL。我要的是http://localhost:49611/SingleSurvey/{my record id}/{my record title}
有什么办法吗?
向 get 方法添加 2 个可为 null 的参数
public ActionResult SingleSurvey(int? id, string title)
{
// if id is not null, get the survey based on id
return View(survey);
}
然后在post方法中,使用参数重定向
[HttpPost]
public ActionResult Create(Domain.Entities.Survey survey)
{
ISurveyRepository surveyRepository = new DbSurveyRepository();
surveyRepository.CreateSurvey(survey);
return RedirectToAction("SingleSurvey", new { id= survey.ID, title = survey.Title });
}
您可能还想定义一条路线,以便 url 是 .../SingleSurvey/someID/someTitle
而不是 .../SingleSurvey?id=someID&title=someTitle
旁注:在创建新调查的情况下,初始化 Survey
的新实例并使用 return View(survey);
而不是 return View()
性能更好
在您的路由配置文件中添加以下路由:
routes.MapRoute(
"SingleSurvey",
"{controller}/{action}/{id}/{title}",
new { controller = "Survey", action = "SingleSurvey", id = UrlParameter.Optional, title = UrlParameter.Optional }
);
然后更新创建操作以将 ID 和标题作为路由值的一部分传递:
[HttpPost]
public ActionResult Create(Domain.Entities.Survey survey)
{
ISurveyRepository surveyRepository = new DbSurveyRepository();
surveyRepository.CreateSurvey(survey);
TempData.Add("surveyID",survey.ID);
return RedirectToAction("SingleSurvey", new { id = survey.Id, title = survey.Title );
}
此外,与其使用 TempData 传递 ID,不如简单地从 URL 中读取 ID。为此,请更新 SingleSurvey
操作以将 ID 作为参数:
public ActionResult SingleSurvey(int? id)
{
if (id != null)
{
ISurveyRepository surveyRepository = new DbSurveyRepository();
Domain.Entities.Survey survey = surveyRepository.GetBySurveyID(id.Value);
return View(survey);
}
return View();
}
MVC框架自动绑定路由中定义的id
参数到action方法的id
参数。如果您需要在 SingleSurvey 操作中使用标题,您还可以将其添加为额外参数。