url在mvc中重写(在url中添加数据库中的字段) [英] Url rewriting in mvc (add a field from database in url)
本文介绍了url在mvc中重写(在url中添加数据库中的字段)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前我的网页网址是这样的
www.something.com/Controller/ActionName/Id
但我需要的是当有人输入此网址时,它应该重定向到www.something.com/Controller/ActionName/Id/Name
目前我正在做的是
[HttpGet]
public ActionResult Details( int id)
{
if (id == null )
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
项目项目= 新项目();
project = GetProjectFromDatabase(Id);
string name = project.Name;
name = name.Replace( , - );
return 重定向( / Project / Details / + id + / + project + / + name); // 此行的问题
i希望此处重定向到第二个操作
}
// 并且还有另一个具有相同名称的控制器
public ActionResult Details( int id,项目项目,字符串 name = )
{
return 查看( 〜/ Views / Project / _Detai ls.cshtml,项目);
}
但当我重定向到另一个具有相同名称的控制器时,它会重定向到自身
有人可以帮我吗
-Thanks
解决方案
这样的地方: -
< pre lang =c#> / Project / Details / + id + / + project + /?name = + name
这不是Id的默认值,所以路由配置会期待那里的价值。
尝试:
return RedirectToAction( action = 详细信息,controller = controllerName, new < /跨度> {ID = ID,项目=项目,姓名=名})
Currently my page url is like that
www.something.com/Controller/ActionName/Id
but what i need is when someone types this url it should redirect to www.something.com/Controller/ActionName/Id/Name
what currently I am doing is
[HttpGet]
public ActionResult Details(int id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Project project = new Project();
project = GetProjectFromDatabase(Id);
string name = project.Name;
name = name.Replace(" ", "-");
return Redirect("/Project/Details/"+id+"/"+project+"/"+name);//Problem at this line
i want here to redirect to second action
}
//and there is another controller with same name
public ActionResult Details(int id, Project project, string name = "")
{
return View("~/Views/Project/_Details.cshtml", project);
}
but when i am redirecting to another controller with same name, it redirects to itself
Can someone help me
-Thanks
解决方案
place it like this :-
"/Project/Details/"+id+"/"+project+"/?name="+name
This is not the default value as Id is, so route config would be expecting a value there.
else try :
return RedirectToAction( action = "Details",controller = controllerName, new{id=id,project=project,name=name})
这篇关于url在mvc中重写(在url中添加数据库中的字段)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文