Return PHP page from ASP route handler - c#

I would like to return a PHP page from a route handler like so:
return BuildManager.CreateInstanceFromVirtualPath("/redirects.php", typeof(Page)) as Page;
This requires an extra buildProvider and returns a page with PHP directives unprocessed, so I can view all the PHP code with View Source. How can I tell it to process the code rather than just output the page?

It's unusual to want ASP.NET and PHP on the same server. The answer is that you need to install the PHP interpreter on your server.
I presume you're using using IIS. This seems like a good place to start: http://php.iis.net/

You would have to call it as a URL. Using CURL or something. I'm not sure what the C# equivalent is.

Related

Accessing Query String with Razor and AngularJs

I am building onto a massive Razor website, which I cannot re-architect. I need to use AngularJs on the client side, and when the page is loaded there is a little bit of server side preprossessing that needs to be done before the page is rendered.
I need to pass a parameter to C# via the URL query string, and I need that same parameter on the JavaScript side. Currently, if I use this URL:
http://localhost:32289/razorPage.cshtml#/?param="1234"
I can get that value on the JavaScript side, but when I call
var queryString = HttpContext.Current.Request.QueryString;
on the server side, it's empty. Additionally, if I use this URL:
http://localhost:32289/razorPage.cshtml/?param="1234"#/
I can access the query string on the server side, but then JavaScript goes nuts, as though I was continuously rerunning the code in my Angular controller. I get this in the Chrome console:
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.
And eventually an error that says the Maximum call stack size has been reached. If I put a console.log() in that Angular controller, it logs continuously until the call stack max size is reached.
This is my razorPage.cshtml:
#{
Page.Title = "OCE Visualizer";
Page.IsDetailedView = false;
Page.IsCapacity = false;
Page.IsEmbedded = false;
InitProvider.Init();
}
<html>
<!--...AngularJs App lives in here-->
</html>
The init method (which populates some data folders on the server so they can be served to Angular) uses parameters from the query string, as does the AngularJs app, which also manages its own routing.
I'm relatively new to Razor, but I am familiar with AngularJs. I think part of the problem could be because of the way .NET manages routing, which could be messing with how Angular can do it. I am aware of this and this SO answers, but they apply to an MVC app, where mine is just a website with a lot of .cshtml pages, no Controllers or APIs.
Is there a way to access query strings in both Angular and Razor C#, while maintaining AngularJs routing with "pretty" URLs?
Ok I figured out a solution. I'll post it if people in the future have this problem, or if it's a bad answer and people can fix it.
I realized that the main difference between the two URLs in my question was the location of the #, which is a fragment identifier (?). I read about it here, but I could caution that that page is almost 20 years old. Anyway, I found that the fragment part of the URL does NOT get sent to the server, which is why I couldn't parse the query string server side when it was after the #. I don't know why JavaScript was freaking out when the query was before the #, but I'm willing to believe it was a problem with my code.
The solution was to pass the query string on both sides of the #. Thus, the working URL looks like this:
http://localhost:32289/razorPage.cshtml?param="1234"#/?param="1234"
The query string to the left is what the C# can access, and the one on the right is what AngularJs can access. Additionally, anything after the # works like normal AngularJs routing, so I don't think that was related.

Is it possible to change the url of WebPage on Page_Load in asp.net?

I am working on a project. Here we are dynamically generating web pages. The web page code renders on Page_Load() and it is generating data based on query string.
I implemented it like
http://www.example.com/Request.aspx?cPanelId=(here query string)
The problem is that Seo will not work on Query string.
If I could change could change the url in this way like
http://www.example.com/(Requested query string Page.aspx)
It will work fine.
Is it implementable?
yes it is implementable in ASP.NET and the name of the concept is URL rewriting. please have a look at the following url(s) for detailed understanding and how to implement it in your project.
http://msdn.microsoft.com/en-us/library/ms972974.aspx
http://weblogs.asp.net/scottgu/tip-trick-url-rewriting-with-asp-net
http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

accessing websites using C#

I have a problem here. Assume there's a basic calculator implemented in javascript hosted on a website ( I have googled it and to find an example and found this one: http://www.unitsconverter.net/calculator/ ). What I want to do is make a program that opens this website, enters some value and gets the return value. So, in our website calculator, the program:
- open the website
- enters an operand
- enters an operation
- enters an operand
- retrieve the result
Note: things should be done without the need to show anything to the user ( the browser for example ).
I did some search and found about HttpWebRequest and HttpWebRespond. But I think those can be used to post data to the server, which means, The file I'm sending data to must be php, aspx or jsp. But Javascript is client side. So, I think they are kind of useless to me in this case.
Any help?
Update:
I have managed to develop the web bot using WebBrowser Control tool ( found in System.Windows.Forms )
Here's a sample of the code:
webBrowser1.Navigate("LinkOfTheSiteYouWant"); // this will load the page specified in the string. You can add webBrowser1.ScriptErrorsSuppressed = true; to disable the script in a page
webBrowser1.Document.GetElementById("ElementId").SetAttribute("HTMLattrbute", "valueToBeSet");
Those are the main methods I have used to do what I wanted to.
I have found this video useful: http://www.youtube.com/watch?v=5P2KvFN_aLY
I guess you could use something like WatiN to pipe the user's input/output from your app to the website and return the results, but as another commenter pointed out, the value of this sort of thing when you could just write your own calculator fairly escapes me.
You'll need a JavaScript interpreter (engine) to parse all the JavaScript code on the page.
https://www.google.com/search?q=c%23+javascript+engine
What you're looking for is something more akin to a web service. The page you provided doesn't seem like it accepts any data in an HTTP POST and doesn't have any meaningful information in the source that you could scrape. If for example you wanted to programmatically make searches for eBay auctions, you could figure out how to correctly post data to it eg:
http://www.ebay.com/sch/i.html?_nkw=http+for+dummies&_sacat=267&_odkw=http+for+dummies&_osacat=0
and then look through the http response for the information you're looking for. You'd probably need to create a regular expression to match the markup you're looking for like if you wanted to know how many results, you'd search the http response for this bit of markup:
<div class="alt w"><div class="cnt">Your search returned <b>0 items.</b></div></div>
As far as clientside/javascript stuff, you just plain aren't going to be able to do anything like what you're going for.
It is a matter of API: "Does the remote website expose any API for the required functionality?".
Well web resources that expose interactive API are called web service. There are tons of examples (Google Maps for istance).
You can access the API -depending on the Terms & Conditions of the service- through a client. The nature of the client depends on the kind of web service you are accessing.
A SOAP based service is based on SOAP protocol.
A REST based service is based on REST principles.
So, if there is an accessible web service called "Calculator", then you can access the service and, for istance, invoke the sum method.
In your example, the calculator is a Javascript implementation, so it is not a web service and it cannot be accessed via HTTP requests. Though, its implementation is still accessible: it is the javascript file where the calculator is implemented. You can always include the file in your website and access its functions via javascript (always mind terms and conditions!!).
A very common example is the jQuery library stored in Google Libraries.

Redirecting url to index.aspx page using standard asp.net3.5 and web.config

I have a subdomain that is http://trade.businessbazaar.in . I am dynamically creating urls from database something in this manner http://trade.businessbazaar.in/mycompany. To display details, I have an index.aspx file there,thinking that on every request the index.aspx page will load and display data accodingly. Also, There is a masterpage on the index.aspx page from where i am capturing the text mycompany and query it in database to fetch result. But nothing seems to work.
A genuine link is http://trade.businessbazaar.in/Symparlife. But its unable to load index.aspx. I need a clean approach without any third party dll or rewriters. Directly to push some lines in config and start working. That is url will be the same but index page will get loaded...
In short, i want to say
I need the StackOverflow type clean url mechanism to fetch pages
Thanks in Advance
You can handle the Begin_Request event in Global.asax and add custom code to redirect to index.aspx and convert the parts of the URL into query string arguments. You should use Server.Transfer to keep the URL in the browser.
I'd recommend upgrading to 4.0 and using the Routing enine though. You should check if the standard routing is available as a download for ASP.NET 3.5. I am sure your code will get messy very soon. Been there, done that.
As #Mike Miller mentions in the comments the Routing engine ships with ASP.NET 3.5. You can check the documentation here - http://msdn.microsoft.com/en-us/library/system.web.routing(v=vs.90).aspx
Here is a tutorial on how to use it with Web Forms - http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx
For your case the code would be something like:
routes.MapPageRoute("company-index", "/{company}", "~/index.aspx")
And in index.aspx you can access the route value for company like this:
string company = (string)Page.RouteData.Values["company"];
Keep in mind that you'd better add something in the URL before your actual argument (the company name). If you don't you will have problems later on when because you may want to add a URL like "/Login" but then you will have to validate that users can't create a company named "Login". Not how Stack Overflow has "/questions/" before the actual question info in the URL.

.net, c# post to php page

I have a website that is primarily PHP but we have built some new pages in c# .net. I need to be able to post (i think) to the PHP page.
The PHP page has a login that takes the login name and password. I am trying to allow my .net page to have a login that directs to the PHP page and once there the user is already logged in.
I believe I can post the data to the PHP page... is that correct?
If so, can someone share a code snippet that points me in the right direction?
Thanks for the help!!
I believe there is a cURL version for c#, the easy way would be to set that up if you can. Some information on using cURL with .NET can be found here and there are a few other resources on Google for c# curl
There are lots and lots of ways. I prefer javascript ( with jquery) ajax calls
$.post('login.php', function(data) {
alert( "returned " + data + " from php page");
});
see jQuery documentation for more details.
If you need to do this serverside (in the c# code behind), let me know, I will write up some sample code.
There's no difference between submitting a form post to a PHP script or to a C# application or to whatever program written in whatever language. How the data is transferred is defined by Internet standards.
Of course, different implementations may have slightly different behaviors. For instance, PHP considers all input names that have square brackets in their name to be part of an array.

Categories

Resources