ASP.NET MVC Url display problem
I have recently been experimenting with the profile features of ASP.N开发者_运维知识库ET. I am having trouble getting a "website" property to display correctly. For example, if the website I enter is:
facebook.com/contactaligand I render it using
<a href="<%: Profile.Website %>"><%: Profile.Website %></a>
it gets rendered on screen as http://localhost:51225/users/facebook.com/contactaligInitially, I thought I might just prepend "http://" if it didn't contain one, but I feel there should be a cleaner solution.
Thanks in advance.
Without the protocol etc it isn't an absolute uri, so the browser (correctly) treats it as relative to the current URL.
So yes: check for a protocol. Perhaps just StartsWith is enough here, else a regex or maybe Uri.TryCreate (or whatever it is) specifying absolute-only.
Personally I would do this check at the point of data-entry, not at display.
精彩评论