For instance if I have http://www.mywebsite.com/about.aspx. Store about.aspx (or whatever page we're on) in a variable. Also need this to work even if there is information after the page in the url such as a query string.
Ah - I suggest you read the ASP.NET documentation VERY carefully. The HttpRequest object, available as Request property contains all information that you can have about the request. This includes the path called, all parameters etc.
Weighing in a little late, but others may get here as I did.
You could try to use a combination of the Uri and Path objects. For example:
Uri test = new Uri("http://www.microsoft.com/default.aspx?id=7&em=958");
Console.WriteLine(Path.GetFileName(test.AbsolutePath));
Not sure if this is the "proper" way but it works for me.
Related
Good evening!
I am currently working on the backend of my application and I need to get a list of all properties of a certain datatype but only the ones in the current page.
listFiltersCms = _umbraco.ContentAtRoot().SelectMany(c => c.Descendants<DataFilters>()).ToList();
This line above gathers all the filters from all the pages, but I want the filters from a specific page (can be currentPage).
I have tried something like this:
var listFiltersCms = _umbraco.AssignedContentItem.SelectMany(c => c.Descendants<DataFilter>()).ToList();
But without any luck :( Any ideas?
Not entirely sure what you mean by "the backend of my application" - are you inside Umbraco or on a public website?
I ask because there is a fairly straightforward way of getting all info on a specific datatype, but it is not really meant to be used on public facing websites. It can be, but it might be taxing on the system as I believe it queries the database directly, which is not ideal.
Anyway, you might want to take a look at the DataTypeService:
https://our.umbraco.com/Documentation/Reference/Management/Services/DataTypeService/Index-v8
And here you can see what options you have for using it (not very explanatory, but I spot a few methods you could probably look into):
https://our.umbraco.com/apidocs/v8/csharp/api/Umbraco.Core.Services.IDataTypeService.html
If you decide to use the service in a scenario where there will be a lot of requests, maybe consider caching the output to spare the database.
The way I solved this issue was by getting the page in the correct model. In my case all I had to do was:
(_umbraco.AssignedContentItem as PageModel).FilterCustomMeasures;
Goal:
Display the url address as "http://localhost:49332/Home/Contact2/1?idd=first" (except "http://localhost:49332") instead of "http://localhost:49332/Home/Contact2/1?one=second&two=yes"(except "http://localhost:49332") when you have pressed the button Create
Problem:
I don't know how to do it? I strongly believe that it has to do with querystring.
Info:
*I would like the querystring to take place inside of the method "Public ActionResult contact2(int? id, string one, string two)"
*You can download the source code on website (https://drive.google.com/file/d/0B23pYZkpAyafbEtYTjhYaHhtNkk/view).
I believe that in your case, the easiest way to do this is using the AttributeRouting.
http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx
Or else, you should configure the routes inside RoutConfig class, this link might be helpful https://msdn.microsoft.com/en-us/library/cc668201%28v=vs.140%29.aspx
If you want to use only querystring, just put the action parameter name in the url... everything should work fine.
I am using C# with ASP.NET.
How do I check if a parameter has been received as a POST variable?
I need to do different actions if the parameter has been sent via POST or via GET.
Use this for GET values:
Request.QueryString["key"]
And this for POST values
Request.Form["key"]
Also, this will work if you don't care whether it comes from GET or POST, or the HttpContext.Items collection:
Request["key"]
Another thing to note (if you need it) is you can check the type of request by using:
Request.RequestType
Which will be the verb used to access the page (usually GET or POST). Request.IsPostBack will usually work to check this, but only if the POST request includes the hidden fields added to the page by the ASP.NET framework.
Use the
Request.Form[]
for POST variables,
Request.QueryString[]
for GET.
In addition to using Request.Form and Request.QueryString and depending on your specific scenario, it may also be useful to check the Page's IsPostBack property.
if (Page.IsPostBack)
{
// HTTP Post
}
else
{
// HTTP Get
}
I have an html helper library that I am making and one of my plugins needs urls to be passed in. I don't want to pass in the full url since they every-time I change something around I have to go and fix all of the urls up.
How can I get a full Url path in my file? Like if I pass in a relative path or something it gets resolved to a full path.
VirtualPathUtility might be a place to look. For example using
VirtualPathUtility.ToAbsolute(src);
will render paths like "~/App/test.jpg" to an absolute location e.g "/VirtualDirectory/App/test.jpg" as well as relative paths. The methods exposed on an instance of the UrlHelper (e.g. Content) class might also be of help.
For future visitors to this thread I use the following code frequently
var baseUrl = HttpContext.Current.Request.Url.AbsoluteUri;
if (HttpContext.Current.Request.Url.LocalPath != "/")
baseUrl = baseUrl.Replace(HttpContext.Current.Request.Url.LocalPath.Substring(1), "");
You can use HttpContext.Current.Server.MapPath(string)
I am currently using a number of query string parameters to pass some data from one page to a second page (the parameters hold confirmation/error messages to display in the second page), that due to a third party product can no longer work correctly in the production environment. The user completes an action on the first page, and is then transferred to the second page. What are the possible alternatives to use instead of a query string and GET - session variables, POST data, or something completely different?
Thanks, MagicAndi.
You could create public properties in a source page and access the property values in the target page when using a server transfer. You could also get control information in the target page from controls in the source page by referencing the Page.PreviousPage property.
Both of these methods are oulined here: http://msdn.microsoft.com/en-us/library/6c3yckfw.aspx
Both POST data and session variables would work just fine. POST data has the drawback that it can be changed by the client and session variables take up memory, so you can choose based on that. I personally don't think that you should pass such messages to the client for the reason stated above but I guess you are already doing that, so...
you can use this if you use window.open("openTheotherPage",...etc)
so form the opened page you can do something like this
var valuefromCallerPage = window.opener.document.FormNmae.textbox.value
or button or anything on the caller page