Trailing slash commonly asked to manage while optimization, as this can treat content duplicate or can make url and seo friendly.
we can obtain it by multiple ways, we can manage url in Action which will called on url browsing or we can use them Global.asax, or we can manage them by customizing inside custom error management classes. Here i have given some examples which can show some scenario of its uses:
1. Using in multiple places (Actions, Classes) in mvc.
if (url.LastIndexOf('/') > 0)
{
Regex reg = new Regex(@"^([^:]\/)\/+$");
string result = reg.Replace(url, "");
string urls= result.Remove(result.Length - 1);
}
else
{
}
2. Using In View In MVC
string url= HttpContext.Current.Request.RawUrl.ToLower().ToString();
string path = HttpContext.Current.Request.Url.Authority;
String Url= ViewBag.Url;
<script type="text/javascript">
var slug = '@Url';
var path = '@path';
var urlredirect = 'http://' + path + '/' + slug;
window.location = urlredirect;
</script>
3. Using In Global.asax file
string requestedUrl = Request.Url.ToString();
if (requestedUrl != null)
{
if (requestedUrl.EndsWith("/"))
{
//Redirect with 301 Status Code if this contain trailing Slashess
requestedUrl = requestedUrl.Substring(0, requestedUrl.Length - 1);
Response.RedirectPermanent(requestedUrl);
}
}
0 comments:
Post a Comment