-1

I am posting some things to the database via AJAX and the C# code in the App_Code folder in my ASP.NET WebPages website is what will be doing this.

My question is, what do I use as the file path for the ajax part?

xmlhttp.open("POST", "App_Code/MyClass/MyMethod()");?

Obviously this doesn't work since it's wrong, but I cannot find any sources to confirm how to do this. I know it can be done because I found a code sample on SO months ago, but I can no longer find it.

Arrow
  • 2,784
  • 8
  • 38
  • 61
  • whats the name space of the file in the app_code folder – MethodMan Oct 30 '12 at 09:06
  • There is no namespace. It's just a .cs Class file, and it begins with `public class JTS`. – Arrow Oct 30 '12 at 09:07
  • James People have every right to downvote if the OP does not show several of things for example doing a google search or one not showing any personal attempt to do this on there own.. I am not sure but I've seen this in the past.. – MethodMan Oct 30 '12 at 13:20
  • 1
    I just looked at your answer and you can access this by creating a NameSpace and including that namespace.foldername.cd file name in your uses clause.. just an fyi – MethodMan Oct 30 '12 at 13:21
  • I never said they didn't have the right. But it's just stupid. If they have such a big problem with somebody's question, the least they could do is say why they have a problem with it. Helps make questions better when you know what you're doing wrong, and btw, I made every reasonable attempt at figuring this out before posting. – Arrow Oct 30 '12 at 13:37
  • James I totally understand.. but there are tons of users that use this and I've seen from my own experience that others will downvote immediately if it appears that a question is too simple to and or if the individual does not show any effort on their part.. there is no reason to get upset.. – MethodMan Oct 30 '12 at 13:40

1 Answers1

2

There is no way to directly request contents in App_code since it's a protected folder.

Instead you should have a .cshtml file that makes use of your class located in the App_code and then call that from your Ajax function. The URL passed to the Ajax call is simply that of the .cshtml file. You can pass parameters to the .cshtml file as form data, query string values or as UrlData.

Mike Brind
  • 28,238
  • 6
  • 56
  • 88
mohsen dorparasti
  • 8,107
  • 7
  • 41
  • 61
  • Will the webmethod page be accessible by the public? – Arrow Oct 30 '12 at 09:10
  • 1
    If you create a service or API, you can demand credentials. One slight hitch will be that if you paste the credentials into your JavaScript anyone will be able to find them. – Fenton Oct 30 '12 at 09:20
  • 1
    @JamesKent : yes , you can read more here : http://encosia.com/asp-net-page-methods-are-only-as-secure-as-you-make-them/ – mohsen dorparasti Oct 30 '12 at 09:23
  • 1
    The answer is incorrect. You cannot use the [WebMethod] attribute in ASP.NET Web Pages. That is for use in Web Forms. And .cshtml files are available over http, as are .cs files. However, .cs files are not served by IIS. cshtml files are, though. – Mike Brind Oct 30 '12 at 13:42
  • @MikeBrind : thank you , I edited the answer to remove incorrect parts .may you check it . – mohsen dorparasti Oct 30 '12 at 17:11
  • 2
    @mohsen.d I have made your answer more relevant to the Web Pages framework. – Mike Brind Oct 30 '12 at 17:23