-
I am new to OrchardCore and I love it so far, after a huge research for which CMS would suit my needs best, I decided to go with Orchard, but I need to learn how to do certain things according to my needs.
Inside Here is an example of Index.cshtml.cs namespace Demo.Pages;
public class HomeIndexModel : PageModel
{
private readonly ILogService _logService;
public HomeIndexModel(ILogService logService)
{
_logService = logService;
}
public async Task<IActionResult> OnGet()
{
// .... Do something
return Page();
}
public async Task<IActionResult> OnGetDataAsync(string value)
{
// Do something else
// var json = ....
return Content(json);
}
public async Task<IActionResult> OnPostVehicleData ([FromBody] InputModel data)
{
// Do something
// var json = await _someService.PostVehicleDataAt(data);
return Content(json);
}
} And this is the function that calls the endpoint in function callSomeApiEndpoint () {
var data = { "item": 1, id: 2}
$.ajax({
type: 'POST',
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
url: '@Url.Page("HomeIndex")?handler=VehicleData',
data: data,
success: function (data) {
var res = JSON.parse(data);
// Do something
},
error: function (err) {
// Toast some info
}
});
} The OnGet method is called with a GET request, but when I try to call other endpoints, it just does not find them. I think it has to do with the way how MapRazorPages() works, and I was looking for a solution since I cannot add My question is how can I achieve the same goal using OrchardCore, after searching in internet I think a solution would be to create a Module, but I did not find much info on how to implement that. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Awesome that you found Orchard! I assume you've seen the decoupled docs page; if not, I suggest you start there. You don't need to call What happens if you just try Yes, you can also add controllers. For Razor Pages, usually you'd add all files into the root web project (under the Pages folder/namespace), and you can similarly add MVC controllers to it. You can also do that in a module indeed. Adding a |
Beta Was this translation helpful? Give feedback.
-
Maybe I came late, but I did a lot of work with OC & RazorPages, so my question why do you need JavaScript? If your answer is AJAX, then why do you need the I usually use the page model to interact with OC APIs (OC.ContentManagement, OC.Media ... etc)
No need to call it as @Piedone mentioned before |
Beta Was this translation helpful? Give feedback.
-
Looks like you need to define route with handler
Also check demo module, to learn more about razor pages route mapping
|
Beta Was this translation helpful? Give feedback.
Awesome that you found Orchard!
I assume you've seen the decoupled docs page; if not, I suggest you start there.
You don't need to call
MapRazorPages()
because that already happens within Orchard. Otherwise, Razor Pages should work exactly the same how they do outside of an Orchard Core-using app.What happens if you just try
OnPost()
?Yes, you can also add controllers. For Razor Pages, usually you'd add all files into the root web project (under the Pages folder/namespace), and you can similarly add MVC controllers to it. You can also do that in a module indeed. Adding a
Manifest
file and a package reference toOrchardCore.Module.Targets
will make a project into a module, what then you c…