使路由成为私有路由,无法通过URL访问

时间:2014-02-23 19:54:24

标签: .net asp.net-mvc asp.net-mvc-4

我正在尝试实施URL Redirect将所有请求重定向到网址,例如例如www.myweb.com/201www.myweb.com/201/my-blog-title

我的代码是这样的:

In controller:

    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}$" });

当网址仅包含ID www.myweb.com/201作为其PostUrlDetermine路由选择并将其指向行动时,上述代码正常工作。但是当网址还包含www.myweb.com/201/my-blog-title这样的标题时,它会直接点击发送视图的Post路由而不会获取信息。

我希望这两个网址都用于PostUrlDetermine路线。

有没有办法可以声明只能从内部访问并且不适用于网址的特定路由?

1 个答案:

答案 0 :(得分:0)

两个路由都需要解析为PostUrlDetermine操作,并进行一些条件检查以查看是否需要重定向。

尝试以下

<强> 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操作有点多余,因此您可以进一步重构以直接从PostUrlDetermine

返回视图
// 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 ...
}