2012-07-27 21 views
1

去除 '/' 如果這是我的網址http://localhost:55070/Server/1-Server如何添加 '/' 到URL,如果用戶從URL中的ASP.NET MVC 4

在該網頁會鏈接成爲

http://localhost:55070/Server/2-Instance_12 

如果我更改這樣的URL http://localhost:55070/Server/1-Server/

鏈接將是這樣的。 http://localhost:55070/Server/1-Server/1-Instance_11

我總是想要seconde類型(http:// localhost:55070/Server/1-Server/1-Instance_11 /)。如果用戶輸入這樣的URL,可能會導致一些問題。

如果這是網址http://localhost:55070/Server/1-Server我該如何追加/到http://localhost:55070/Server/1-Server/。所以下一個視圖中的鏈接也會附加到它。

用戶可以刪除'/',然後我需要添加'/'。

這是創建URL的Razor。

+0

說些什麼?我認爲你的鏈接可能還有其他問題。你能解釋一下你想要完成什麼嗎? – Eonasdan 2012-07-27 11:42:08

回答

2

你能適應這種解決方案Lower case URLs in ASP.NET MVC重定向當URL沒有以/

最終還是把它放在一個HTTP模塊,像這樣的用戶:

public class UrlMessingModule : IHttpModule 
{ 
    public void Init(HttpApplication context) 
    { 
     context.BeginRequest += Application_BeginRequest; 
    } 

    public void Dispose() { } 

    protected void Application_BeginRequest(object sender, EventArgs e) 
    { 
     var application = (HttpApplication) sender; 
     var request = application.Request; 
     var response = application.Response; 

     var url = request.Url.AbsolutePath; 

     if (url.Length > 1 && !url.EndsWith("/")) 
     { 
      response.Clear(); 
      response.Status = "301 Moved Permanently"; 
      response.StatusCode = (int)HttpStatusCode.MovedPermanently; 
      response.AddHeader("Location", url + "/"); 
      response.End(); 
     } 

    } 
} 
相關問題