我正在开发一个网站,其中包含一些使用MVC4和实体框架的教程。所有教程都将保存在数据库中,根据请求URL中提供的教程Id(例如TId),它将在操作方法中检索有关该教程的所有信息,并在视图中呈现并显示给用户。下面是一个示例URL。
的URL: www.mysite.com/Tutorials/Show/452
其中Tutorials为Controller Name, Show为action method Name。
这里452是TId。因此,当这个URL被请求时,将显示带有TId 452的教程。但是我想要的是,我想在教程的末尾加上虚线,如下所示。
www.mysite.com/Tutorials/Show/452/My-Test-Tutorial
我可以用'-'替换空格并生成字符串,但我找不到将其附加到URL的方法。
这与stackoverflow网站完美配合。例如,即使我们请求"在MVC4,如何重定向到一个视图从一个控制器的动作在Url中的参数?" Id "20035665"的问题正在显示,URL将更改为"在MVC4中,如何从控制器动作重定向到视图与URL中的参数?".
有谁能帮我一下吗?假设我正确理解了你的问题,下面是我提出的解决方案:
添加以下更改到路由机制:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Tutorials",
url: "Tutorials/{id}/{title}",
defaults: new { controller = "Tutorials", action = "Show", title = UrlParameter.Optional },
constraints: new { id = @"d+" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
,然后在你的教程控制器:
public class TutorialsController : Controller
{
// GET: /Tutorials
public ActionResult Index()
{
// here you can display a list of the most recent tutorials or whatever
return View();
}
// GET: /Tutorials/1 (will be redirected to the one below)
// GET: /Tutorials/1/Intro-into-ASP_NET-MVC
public ActionResult Show(int id, string title)
{
string key = string.Format("tutorial-{0}", id.ToString());
Tutorial model = TempData[key] as Tutorial;
// if this is not a redirect
if ( model == null )
{
model = GetTutorial(id);
}
// sanitize title parameter
string urlParam = model.Title.Replace(' ', '-');
// apparently IIS assumes that you are requesting a resource if your url contains '.'
urlParam = urlParam.Replace('.', '_');
// encode special characters like '', '/', '>', '<', '&', etc.
urlParam = Url.Encode(urlParam);
// this handles the case when title is null or simply wrong
if ( !urlParam.Equals(title) )
{
TempData[key] = model;
return RedirectToAction("Show", new { id = id, title = urlParam });
}
return View(model);
}
private Tutorial GetTutorial(int id)
{
// grab actual data from your database
Tutorial tutorial = new Tutorial { Id = 1, Title = "Intro into ASP.NET MVC" };
return tutorial;
}
}
更新:上面给出的解决方案将重定向
/教程/1
/教程/1/Intro-into-ASP_NET-MVC 。
如果你真的想在url中显示动作名称,比如/Tutorials/Show/1/Intro-into-ASP_NET-MVC你可以把"Tutorials"路由中的"url"改为url: "Tutorials/Show/{id}/{title}"
。
你也可以用RedirectToRoute("Default", new { id = id, title = urlParam });
替换RedirectToAction,这将确保它匹配名为"Default"的路由,但这种方法会产生以下url: www.mysite.com/Tutorials/Show/1?title=Intro-into-ASP_NET-MVC
你可以有一个路由配置如下,当你创建URL时,你可以将教程标题传递给一个方法,该方法将给定的文本转换为URL友好文本。
路由配置
routes.MapRoute(
name: "Tutorials",
url: "{controller}/{action}/{tid}/{title}",
defaults: new { controller = "Tutorials", action = "Show", tid = UrlParameter.Optional, title = UrlParameter.Optional }
);
在View中创建url
<a href='@Url.Action("Tutorials", "Show", new { tid = tutorial.ID, title = ToFriendlyUrl(tutorial.Title) })'>My Tutorial</a>
显示方法
public ActionResult Show(int tid, string title)
{
// if the title is missing you can do a redirect inside action method
return View();
}