.net 将路由设置为私有且无法通过URL访问
我正在尝试实现一个.net 将路由设置为私有且无法通过URL访问,.net,asp.net-mvc,asp.net-mvc-4,.net,Asp.net Mvc,Asp.net Mvc 4,我正在尝试实现一个URL重定向,将所有请求重定向到一个URL,例如www.myweb.com/201到www.myweb.com/201/my blog title: 我的代码如下所示: 控制器中的: public ActionResult PostUrlDetermine(int pId) { ... TempData["postInfo"] = postInfo[]; if (postInfo != nu
URL重定向
,将所有请求重定向到一个URL,例如www.myweb.com/201
到www.myweb.com/201/my blog title
:
我的代码如下所示:
控制器中的:
public ActionResult PostUrlDetermine(int pId)
{
...
TempData["postInfo"] = postInfo[];
if (postInfo != null)
{
int urlTitleLength = 50;
string urlPostTitle = "";
if (postInfo[1].Length > urlTitleLength)
urlPostTitle = postInfo[1].Substring(0, urlTitleLength);
else
urlPostTitle = postInfo[1];
urlPostTitle = urlPostTitle.Replace(' ', '-');
return RedirectToAction("Post", new { pId = pId, postTitle = urlPostTitle });
}
else if (postInfo == null)
{
return RedirectToRoute("Blog");
}
return RedirectToAction("Post", new { pId = pId });
}
public ActionResult Post(int pId, string postTitle)
{
return View();
}
Route.config
routes.MapRoute("PostUrlDetermine", "{pId}", new { controller = "Blog", action = "PostUrlDetermine", }, new { pId = @"^\d{1,3}$" });
routes.MapRoute(name: "Post", url: "{pId}/{postTitle}", defaults: new { controller = "Blog", action = "Post", postTitle = UrlParameter.Optional}, constraints: new { pId = @"^\d{1,3}$" });
当URL仅包含IDwww.myweb.com/201
作为其选取时,上述代码可以正常工作posturlDeterminate
路由并将其指向动作和所有内容。但是,当URL也包含像www.myweb.com/201/my blog title
这样的标题时,它会直接点击Post
路径,发送视图而不获取信息
我希望两个URL都指向posturldefinect
route
是否有一种方法可以声明只能从内部访问且不适用于URL的特定路由?两个路由都需要解析为
posturldefinect
操作,并执行一些条件检查以查看是否需要重定向
试试下面的方法
Global.asax
routes.MapRoute(
"Post",
"{pId}/{postTitle}",
new { controller = "Blog", action = "PostUrlDetermine", postTitle = UrlParameter.Optional },
new { pId = @"^\d{1,3}$" }
);
BlogController
public ActionResult PostUrlDetermine(int pId, string postTitle)
{
// check viewdata/postinfo
string[] postInfo = (string[])TempData["postInfo"] ?? // get post info here;
// no post data redirect to blog
if (postInfo == null)
return RedirectToRoute("Blog");
TempData["postInfo"] = postInfo;
// check posttitle
if (string.IsNullOrWhiteSpace(postTitle))
{
int urlTitleLength = 50;
string urlPostTitle = "";
if (postInfo[1].Length > urlTitleLength)
urlPostTitle = postInfo[1].Substring(0, urlTitleLength);
else
urlPostTitle = postInfo[1];
urlPostTitle = urlPostTitle.Replace(' ', '-');
// viewdata no posttitle redirect
return RedirectToAction("PostUrlDetermine", new { pId, postTitle = urlPostTitle });
}
// viewdata and posttitle exist return view
return Post();
}
public ActionResult Post()
{
return View("Post");
}
这使得Post
操作有些冗余,因此您可以进一步重构,直接从posturldefinect
// viewdata and posttitle exist return view
return View("Post");
更新
if (string.IsNullOrWhiteSpace(postTitle) || postInfo[1].Split(' ')[0] != postTitle.Split('-')[0])
{
...
// viewdata no posttitle redirect
return RedirectToAction ...
}
我不清楚你想干什么。所有这些的目的是什么?实现一个与
www.myweb.com/201
和www.myweb.com/201/my blog title
相同的路径。。就像StackOverflowhttp://stackoverflow.com/questions/21973668
和http://stackoverflow.com/questions/21973668/making-a-route-a-private-and-not-accessible-through-urls
两者都会将您带到同一页面。在这种情况下,您应该使用301重定向,而不是302重定向。如果您将RouteBase子类化,这可能更容易实现,因为您也可以控制URL的构造方式。我如何实现302
重定向,请您解释一下,我对此有点陌生。这在测试中似乎对我有用。你在打哪一行?如果您正在点击最后一个返回RedirectToAction(“Post”,new{pId=pId})
然后重定向循环将继续,因为没有定义postTitle
。大概如果你碰到了这一点,那么博客文章就不存在了?正如我在问题中所说的那样,url的标题类似于www.myweb.com/201/my blog title
,它直接命中了发送视图而不获取信息的帖子路径。我想你们的两条路线也定义了同一件事。很抱歉,我想我一开始误解了,我已经编辑了我的答案。如果这有帮助,请告诉我。好的,它可以工作,但现在的问题是它将与任何第二个参数一起工作,例如www.myweb.com/201/my blog title
或www.myweb.com/201/asklfjklasjfk
都是相同的。但是,我只想使用正确的博客标题重定向?您需要在操作中添加另一个条件,以根据postInfo
值验证postitle
。请参阅更新