asp.net mvc - 返回视图后重写Url

时间:2022-12-12 11:22:16

I have a problem like this. In RouteConfig.cs, I set routes

我有这样的问题。在RouteConfig.cs中,我设置了路由

routes.MapRoute(
      "NewsDetails",
      "news/details-news/{title}-{id}",
      new { controller = "News", action = "Details", id = "", title = "" }
);

In my Index.cshtml of NewsController I have a link

在我的NewsController的Index.cshtml中,我有一个链接

@Html.RouteLink(item.Title, "NewsDetails", new { 
         title = MyWeb.Classes.PrettyUrlHelper.PrettyUrl(item.Title), 
         id = item.Id 
})

In my NewsController:

在我的NewsController中:

public ActionResult Details(string title,String id)
{
    if (id == null && title == null)
       return RedirectToAction("Index");


     try
     {
        int ID = Int32.Parse(id);

        var result = NewsConnectionDB.GetInstance().Single<LifeStory>(ID);

        return View(result);
      }

      catch (InvalidOperationException) { 
          return  View("~/Views/Error/Error404.cshtml"); 
      }
      catch (FormatException) { 
          return View("~/Views/Error/Error404.cshtml"); }
 }

So if a user click on link in View, that link will route to action Details to process, and the link is Seo Url Friendly (localhost:9224/news/details-news/ten-things-2). But a user types a link instead of clicking to a link in View:

因此,如果用户单击View中的链接,该链接将路由到要处理的操作详细信息,并且该链接是Seo Url Friendly(localhost:9224 / news / details-news / ten-things-2)。但是用户键入链接而不是单击View中的链接:

  localhost:9224/news/details-news/ten-thingsblahblahblah-2

The url above is correct with id but title is not. So how can I update the url after I return View if a user types the wrong title but right id?

上面的url是正确的id,但title不是。那么如果用户输入错误的标题但是正确的ID,我如何在返回View后更新网址?

Any help would be appreciated.

任何帮助,将不胜感激。

P/S: my English is not good, so I hope you understand it.

P / S:我的英语不好,所以我希望你能理解。

2 个解决方案

#1


If title is incorrect then you can send correct url in response headers. If it's ajax call then on completion check for correct url in response header. If correct url exists then change your browser url using window.history.pushState javascript method.

如果标题不正确,那么您可以在响应标头中发送正确的URL。如果它是ajax调用,则在完成时检查响应头中的正确url。如果存在正确的URL,则使用window.history.pushState javascript方法更改浏览器URL。

In Details action method use below code to set response header.

在详细信息操作方法中使用以下代码来设置响应标头。

HttpContext.Current.Response.AppendHeader("CorrectUrl", "YourUrl");

#2


Use HttpServerUtility.UrlEncode(string);

javascript code can be replace url, I think it will be working :).

javascript代码可以替换url,我认为它会工作:)。

C# code

string _entitle = HttpServerUtility.UrlEncode(_strTitle);
string _strCorUrl = "http://example.com/"+ _entitle + "-" + _intID.toString();

script code

top.window.location.replace('CorrectUrl');

or C# code redirect url

或C#代码重定向网址

Response.Redirect(url);

Update possible 1 solution with Context.RewritePath

使用Context.RewritePath更新可能的1个解决方案

https://msdn.microsoft.com/en-us/library/sa5wkk6d(v=vs.110).aspx

void Application_BeginRequest(Object sender, EventArgs e)
{
    string originalPath = HttpContext.Current.Request.Path.ToLower();
    if (originalPath.Contains("/page1"))
    {
        Context.RewritePath(originalPath.Replace("/page1", "/RewritePath.aspx?page=page1"));
    }
    if (originalPath.Contains("/page2"))
    {
        Context.RewritePath(originalPath.Replace("/page2", "/RewritePath.aspx"), "pathinfo", "page=page2");
    }
}  

It code is example, You can use it I hope it help

它代码是示例,您可以使用它我希望它有所帮助

#1


If title is incorrect then you can send correct url in response headers. If it's ajax call then on completion check for correct url in response header. If correct url exists then change your browser url using window.history.pushState javascript method.

如果标题不正确,那么您可以在响应标头中发送正确的URL。如果它是ajax调用,则在完成时检查响应头中的正确url。如果存在正确的URL,则使用window.history.pushState javascript方法更改浏览器URL。

In Details action method use below code to set response header.

在详细信息操作方法中使用以下代码来设置响应标头。

HttpContext.Current.Response.AppendHeader("CorrectUrl", "YourUrl");

#2


Use HttpServerUtility.UrlEncode(string);

javascript code can be replace url, I think it will be working :).

javascript代码可以替换url,我认为它会工作:)。

C# code

string _entitle = HttpServerUtility.UrlEncode(_strTitle);
string _strCorUrl = "http://example.com/"+ _entitle + "-" + _intID.toString();

script code

top.window.location.replace('CorrectUrl');

or C# code redirect url

或C#代码重定向网址

Response.Redirect(url);

Update possible 1 solution with Context.RewritePath

使用Context.RewritePath更新可能的1个解决方案

https://msdn.microsoft.com/en-us/library/sa5wkk6d(v=vs.110).aspx

void Application_BeginRequest(Object sender, EventArgs e)
{
    string originalPath = HttpContext.Current.Request.Path.ToLower();
    if (originalPath.Contains("/page1"))
    {
        Context.RewritePath(originalPath.Replace("/page1", "/RewritePath.aspx?page=page1"));
    }
    if (originalPath.Contains("/page2"))
    {
        Context.RewritePath(originalPath.Replace("/page2", "/RewritePath.aspx"), "pathinfo", "page=page2");
    }
}  

It code is example, You can use it I hope it help

它代码是示例,您可以使用它我希望它有所帮助