How do I create a url with a bookmark? - c#

I want to have a link to the knowledge base but I want to also create the bookmark using the id attribute.
// var url = "/KnowledgeBase/Index#Bookmark"
so in my view I want to add that bookmark using the value from my model
var url = Url.Content(string.Concat(Url.Action("Index", "KnowledgeBase"), "#", Model.Bookmark));
This doesn't seem elegant. Is there a better way?

This works for me:
TextLink

Try this,
#Html.Action("Index","KnowledgeBase",new{id= Model.Bookmark})
or you have to redirect using script
window.location.href = '#Url.Action( "Index", "KnowledgeBase" )?id='+#Model.Bookmark

Related

reading querysting from page url inside controller action result?

In MVC controller I need to read GUID which is part of URL. Below is my URL:
http://localhost:56445/Dir1/SubDir1/f3164d1b-38bf-4811-9327-a24200c7614b
Or it can be like:
http://localhost:56445/Dir1/f3164d1b-38bf-4811-9327-a24200c7614b
I am aware of Request.UrlRefferal but I want to avoid parsing. Is there any way I can read it please
var guid =urlString.Split('/').Last()
OR
Uri uri = new Uri(urlString);
string guid = uri.Segments.Last();
if you want to use Request.UrlReferrer then
string guid = Request.UrlReferrer.Segments.Last();
If you are using ASP.NET MVC, Action method for that view is already capable of accepting the
public ResultAction Detail(string guid){
//use guid here.
}

redirection to other page with encoded url

I am using one Image on my html page. where i am adding one anchor to that image to give the link.
that anchor contains href as ~/LB/lct.aspx?pid=177&cat=Happily In Love
Main Thing This URL IS COMING FROM DATABASE. Manually i am not entering it..
So its a Invalid URL Because of spaces between Happily In Love
I used Httputility.urlencoding and decoding also........but the problem i am facing is that..
Url is endoded properly but while i am clicking on the image its not redirecting to proper page because encoded url is not decoded..
How to resolve this...pls help me on this....
Here is the code
string url = "~/LB/lct.aspx?pid=177&cat=Happily In Love"; //your input
string[] arr = url.Split('?');
var nameValues = HttpUtility.ParseQueryString(arr[1]);
foreach (var n in nameValues.AllKeys)
{
nameValues.Set(n, HttpUtility.UrlEncode(nameValues[n]));
}
url = arr[0] + "?" + nameValues.ToString(); //your output
Use the following code to decode Querystring values
string cat = HttpUtility.UrlDecode(Request.QueryString["cat"].ToString());
Simple answer, but if it's just the spaces that are causing problems, consider replacing them with a + using a string replace:
string newurl = url.Replace(" ","+");
Note that this is only really safe if the spaces are restricted to the contents of a querystring.

Get site url on mvc

I want to write a little helper function that returns the site url.
Coming from PHP and Codeigniter, I'm very upset that I can't get it to work the way I want.
Here's what I'm trying:
#{
var urlHelper = new UrlHelper(Html.ViewContext.RequestContext);
var baseurl = urlHelper.Content("~");
}
<script>
function base_url(url) {
url = url || "";
return '#baseurl' + url;
}
</script>
I want to return the base url of my application, so I can make ajax calls without worrying about paths. Here's how I intend to use it:
// Development
base_url(); // http://localhost:50024
// Production
base_url("Custom/Path"); // http://site.com/Custom/Path
How can I do something like that?
EDIT
I want absolute paths because I have abstracted js objects that makes my ajax calls.
So suppose I have:
function MyController() {
// ... js code
return $resource('../MyController/:id');
}
// then
var my_ctrl = MyController();
my_ctrl.id = 1;
my_ctrl.get(); // GET: ../MyController/1
This works when my route is http://localhost:8080/MyController/Edit but will fail when is http://localhost:8080/MyController .
I managed to do it like this:
#{
var url = Request.Url;
var baseurl = url.GetLeftPart(UriPartial.Authority);
}
Thank you all!
Are you aware of #Url.Action("actionname") and #Url.RouteUrl("routename") ?
Both of these should do what you're describing.
Instead of manually creating your URL's, you can use #Url.Action() to construct your URLs.
<p>#Url.Action("Index", "Home")</p>
/Home/Index
<p>#Url.Action("Edit", "Person", new { id = 1 })</p>
/Person/Edit/1
<p>#Url.Action("Search", "Book", new { title = "Gone With The Wind" })</p>
/Book/Search?title="Gone+With+The+Wind"
Now the absolute best reason to go with this option is that #Url.Action automatically applies any vanity URL routes you have defined in your Global.asax file. DRY as the sub-saharan desert! :)
In your case, your can create a 'custom path' in two ways.
Option A)
<p>#Url.Action("Path", "Custom")</p>
/Custom/Path
Option B)
You can create a route using the Global.asax file. So your controller/action combo can be anything you want, and you can create a custom vanity route url - regardless of the controller/action combo.

Input URL like http://example.com changes to http:/example.com in action input

I asked a question to get URL as action input here. Now I have a new problem. The passed URL to action changes from http://example.com to http:/example.com.
I want to know why and how can I resolve the problem.
P.S: I added this code to resolve but I think there may be another problems in future! the code is:
if ((url.Contains(":/")) && !(url.Contains("://")))
{
url = url.Replace(":/", "://");
}
The browser (or server) is replacing a double slash (illegal) with a single one.
Try it,
http://stackoverflow.com/questions/11853025//input-url-like-http-site-com-changes-to-http-site-com-in-action-input
(in Chrome) goes to:
http://stackoverflow.com/questions/11853025/input-url-like-http-site-com-changes-to-http-site-com-in-action-input
If I were you, I would remove the http:// from your path and add it later.
http://localhost:1619/Utility/PR/example.com/
Then, url = "http://" + url;
If you might get secure urls, add that to the route /http/example.com or /https/example.com
use regex:
string src = #"http://example.com";
string result = Regex.Replace(src, #"(?<=https?:/)/", "");
if you need to revert:
string src = #"http:/example.com";
string result = Regex.Replace(src, #"(?<=https?:)/(?=[^/])", #"//");

Redirecting to AND posting data to an external page

I have a search page on my .NET 3.5 Web Forms site that redirects a user to an external site based on the user's search parameters. I would redirect to: http://www.site.com/search.aspx?searchterm=Hello.
But now they are changing the site so that the search parameter is passed as a POST parameter, and not in the query string. So the page is expecting "searchterm".
So not only do I need to redirect to the external page, I have to post data to the page as well. I have no idea how to do this and I don't know where to start.
Is this something I can do in Web Forms without some glitchy workaround? Or maybe it can be done using jQuery?
Most browsers will explicitely deny this. Doing a cross server post like this would lead to security issues.
You can create simple JavaScript function for execute POST redirect to external page (dynamicaly generate and initilaze form object and submit it).
For example (values pattern: a=1&b=2&c=3 ...):
function bind(pageURL, values) {
var form=document.createElement('form');
form.action= pageURL;
form.target='_blank';
form.style.display = 'none';
form.method = 'POST';
var valuesSplit = node.get_value().toString().split("&");
for (var i = 0; i < valuesSplit.length - 1; i++) {
var p = valuesSplit[i];
var ps = p.split('=');
addParam(form, ps[0], ps[1]);
}
document.body.appendChild(form);
form.submit();
}
function addParam(form,key,value){
var input= document.createElement('input');
input.name=key;
input.value=value;
form.appendChild(input);
}

Categories

Resources