1

Controller action method-

public ActionResult Index()
{
   ViewBag.BaseUrl = "http://localhost:50926/";
   return View();
}

In ViewPage-

<a href="@ViewBag.BaseUrl/page.html">One</a>

This generates url as follows (notice the double slash at end)

"http://localhost:50926//page.html" 

How to get rid of double slash? Please note that BaseUrl value can not be changed.

1
  • You can write some extension method which will remove all extra slashes and use it everywhere in the project. Do you need an example? Commented Dec 23, 2015 at 12:30

3 Answers 3

2

I suppose this is what you're looking for:

<a href="@(ViewBag.BaseUrl+"page.html")">One</a>
Sign up to request clarification or add additional context in comments.

3 Comments

I think it's not 100% safe. If ViewBag.BaseUrl doesn't contain ending slash then broken URL will be generated. This behavior is much more worse then double slashes
@dimonser I'm not sure what do you expect from this code. Every peace of code can broke on some conditions. But i don't think that you should write tons of code to avoid all errors. It's just not worth it.
I think we should write safe code, especially in this case. Broken URLs generate angry users.
1

try this in your view :

@{
     Uri baseUri = new Uri(ViewBag.BaseUrl);
     Uri myUri = new Uri(baseUri, "/page.html");
 }

and then use @myUri every place you want.

Comments

0

The solution is to avoid the extra slash before page.html. But you can't leave this text just near the variable. You must do that way :

<a href="@(ViewBag.BaseUrl)page.html">One</a>

In this case you will get "http://localhost:50926/page.html" without the unexpected double slash.

1 Comment

I like your answer. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.