[英]change URL of route in asp.net mvc 4
In my application I am creating a record in one view & displaying it in other view. 在我的应用程序中,我正在一个视图中创建一条记录,并在另一视图中显示它。 bellow are controller actions
下面是控制器的动作
[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();
}
There are two views 1. Create 2. SingleSurvey 有两个视图1.创建2. SingleSurvey
Now when I return view "SingleSurvey" from action "SingleSurvey" the URL displayed on browser is http://localhost:49611/SingleSurvey
. 现在,当我从操作“ SingleSurvey”返回视图“ SingleSurvey”时,浏览器上显示的URL是
http://localhost:49611/SingleSurvey
。
But I want to change this URL. 但是我想更改此URL。 What I want is
http://localhost:49611/SingleSurvey/{my record id}/{my record title}
我想要的是
http://localhost:49611/SingleSurvey/{my record id}/{my record title}
Is there any way to do this ? 有什么办法吗?
In your route config file add the following route: 在您的路由配置文件中,添加以下路由:
routes.MapRoute(
"SingleSurvey",
"{controller}/{action}/{id}/{title}",
new { controller = "Survey", action = "SingleSurvey", id = UrlParameter.Optional, title = UrlParameter.Optional }
);
Then update the create action to pass the ID and title as part of the route values: 然后更新创建动作以将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 );
}
In additon, rather than using the TempData to pass the ID it is better to simply read the ID from the URL. 此外,与其简单地从URL读取ID,不如使用TempData传递ID。 To do this, update the
SingleSurvey
action to take in the ID as a parameter: 为此,请更新
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();
}
The MVC framework automatically binds the id
parameter defined in the route to the id
parameter on the action method. MVC框架会自动将路由中定义的
id
参数绑定到action方法上的id
参数。 If you need to use the title in the SingleSurvey action you can also add it as an extra parameter. 如果需要在SingleSurvey操作中使用标题,则也可以将其添加为附加参数。
Add 2 nullable parameters to the get method 将2个可为空的参数添加到get方法
public ActionResult SingleSurvey(int? id, string title)
{
// if id is not null, get the survey based on id
return View(survey);
}
then in the post method, redirect with the parameters 然后在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 });
}
You might also want want to define a route so that the url is .../SingleSurvey/someID/someTitle
rather than .../SingleSurvey?id=someID&title=someTitle
您可能还想定义一个路由,以便url为
.../SingleSurvey/someID/someTitle
而不是.../SingleSurvey?id=someID&title=someTitle
Side note: Its better performance to initialize a new instance of Survey
and use return View(survey);
旁注:更好的性能来初始化
Survey
的新实例并使用return View(survey);
rather than return View()
in the case where you are creating a new survey 而不是在创建新调查的情况下
return View()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.