I had a razor page called Submit that was giving me a Value cannot be null. Parameter name : viewData error so I removed the #page at the top of my code as this post directed. My issue now is that when I load the page I the following error:
InvalidOperationException: The relative page path 'Index' can only be used while executing a Razor Page. Specify a root relative path with a leading '/' to generate a URL outside of a Razor Page. If you are using LinkGenerator then you must provide the current HttpContext to use relative pages.>
I want my page to have a URL of https://localhost:44369/Submit How would I do this? Here is my routing in my Startup.cs file:
app.UseMvc(routes =>
{
routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
The reason you are getting the error is because of incorrect syntax. In one of your Razor Views, you probably have an anchor tag that might look like:
<a asp-page="Index">Index</a>
That syntax (without the forward slash /)is meant for a Razor Page. In the Razor View, prefix the Page name with a forward slash:
<a asp-page="/Index">Index</a>
If everything else is correct you should be able to browse https://localhost:44369/Submit and it should show the whatever is written in your Submit page
You might use Razor Pages asp-action in a MVC project.
For MVC
1> Using tag asp-action but not asp-page to redirect.
<a asp-action="Submit" class="btn btn-success form-control">Submit</a>
2> use Route Attribute to route to the specified view .
Controller.cs
public class HomeController : Controller
{
[Route("/Submit")]
public IActionResult Submit()
{
return View("~/Views/Home/Submit.cshtml");
}
}
Test:
https://localhost:44307/Submit
https://localhost:44307/submit
The screenshot of result:
There are some articles it might help you to figure out Razor Pages and MVC.
How Does Razor Pages Differ From MVC In ASP.NET Core?
Building Your .NET App - Razor Pages vs. ASP.NET MVC
Related
I am converting some MVC example code to use in a Razor pages application I am building. In the original MVC code, some of the links use the following asp helpers (for instance):
<a asp-controller="Home" asp-action="Preview" asp-route-id="option">A link</a>
In the original MVC code there is a HomeController controller class defined.
I can get this to work in my Razor pages app if I have a page called 'Preview' in a directory called 'Home' under the 'Pages' folder. I then map the route using the #pages directive putting
#page "{option}"
at the top of the 'Preview.cshtml' page and passing a string to the OnGet method in the class definition of PreviewModel in the Preview.cshtml.cs file.
This works OK but is it what I should be doing? There is no HomeController in the Razor pages application, so is there a best practice Razor pages way of doing this?
You can't rely on the combination of the controller and action to always generate the correct route. You should change the tag helper to use the asp-page attribute instead.
<a asp-page="/Home/Preview" asp-route-id="option">A link</a>
See more about routing in Razor Pages here (my site): https://www.learnrazorpages.com/razor-pages/routing
RedirectToPage("Companies") will redirect to /Pages/Companies.cshtml (from an ASP.NET MVC controller)
But what if want redirect to this page /Areas/MyArea/Pages/Companies.cshtml ?
All those and many others don't work:
RedirectToPage("/MyArea/Companies.cshtml")
RedirectToPage("MyArea/Companies.cshtml")
RedirectToPage("./MyArea/Companies.cshtml")
RedirectToPage("/MyArea/Companies")
RedirectToPage("MyArea/Companies")
RedirectToPage("./MyArea/Companies")
Sometimes I get "Page not found" error. Sometimes get "Specify a root relative path with a leading '/' to generate a URL outside of a Razor Page". There are no Pages folder. I know all this can change all rules again.
P.S. Razor pages configred with plain .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); no specific routing added.
Use the overload of RedirectToPage that takes an object representing RouteValues:
return RedirectToPage("/Companies", new { area = "MyArea" });
Note that the '/' is required if you use RedirectToPage in a controller (or anywhere outside of a Razor Page). Otherwise it is not required (but will still work).
This works for me:
return RedirectToPage("/Companies", new { area = "MyArea" });
Works under plain .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); no specific routing configured.
I feel this will be a popular question... Thanks to Mike Bring, he show me a path.
P.S. If you have Pages folder - all rules will be changed once more. That is the way how "Razor Pages" try to run out from "MVC magic"
I am working on Asp.net MVC 4 with Razor. The urls of my projects are like
localhost:port/category1
localhost:port/category2 etc while my controller is home and action is MyCustomAction
I am using Paging on Category1 page with ul li and anchor tag with href="javascript:ShowPage(pagenmbr)"
And Code of ShowPage function is $('#currentpage').val(pagenmbr); $('#myform').submit();
where currentpage is hidden field on my cshtml page
My form is
#using(Html.BeginForm("home","MyCustomAction",FormMethod.Post,new { #id="myform"})
and as i pressed any link for any page functionality is working properly
but in that case my url becomes
localhost:port/category1?controller=home&action=MyCustomeAction
while I don't want this url pattern url should be
localhost:port/category1
Am I doing something in a wrong way, please help me.....
please help me out
Obviously, there's something wrong in your routing. Try posting the code from your Route registering class.
Also for the below line:
#using(Html.BeginForm("home","MyCustomAction",FormMethod.Post,new { #id="myform"})
Try switching this to:
<form method="post" id="myform">
Doing this, will force the form to always submit to the current Url.
And you can't use the HtmlHelper to generate this as you need to specify the htmlAttributes which can only be passed after the actionName and the controllerName.
I'm totally new to AngularJS. Just wanted to try out relative URLs with $routeProvider.
Here is the scenario:
I have a "Edit" page, the ASP.NET MVC link to the page would be:
http://localhost/Workflow/Edit
So the ASP.NET MVC Controller is "WorkflowController" and the action is "Edit". For the Partials I have Controller actions that each return a Partial View like this:
public ActionResult WorkflowTransition()
{
return PartialView("WorkflowTransition");
}
public ActionResult WorkflowTransitionApprovers()
{
return PartialView("WorkflowTransitionApprovers");
}
Following is the AngularJS module configuration - note: a PartialView (as mentioned above) is called for each of the routes (this could be incorrect):
$routeProvider.when('Workflow/WorkflowTransition', {
templateUrl: '/Workflow/WorkflowTransition',
controller: 'transitionCtrl',
});
$routeProvider.when('Workflow/WorkflowTransitionApprovers', {
templateUrl: '/Workflow/WorkflowTransitionApprovers',
controller: 'approversCtrl'
$routeProvider.otherwise({
redirectTo: '/'
});
Note: I have not specified either of the following:
$locationProvider.html5Mode(false);
OR
$locationProvider.html5Mode(false).hashPrefix('!');
The href links are specified like this:
{{workflow.Identifier}}
This generates links of this form:
http://localhost/Workflow/Edit#/Workflow/WorkflowTransition
This is obviously wrong (and clicking the link doesn't do anything probably because the browser tried to navigate to the hash), so I have tried the leading '/', but no luck there too:
{{workflow.Identifier}}
If I navigate the partial directly i.e. http://localhost/Workflow/WorkflowTransition, the browser renders the html as-is (along with the angularjs curly braces {{}}).
My question is: How does AngularJS treat the '#' or '#!' when it comes to determining relative URLs? For e.g. does this route (assuming I knock off the /Edit part from the URL in the anchor tag):
$routeProvider.when('Workflow/WorkflowTransition', match the URL:
http://localhost/Workflow/#WorkflowTransition ?
Does it remove the '#' from the URL and then check it against the URL pattern in $routeProvider.when() ?
Can someone please suggest correct routes for the relative URLs?
From my observation, the hash has to be removed when matching routes. I have a link like
<a href="#Objects">
resulting in the URL
http://localhost:55033/#/Objects
My route Setup is
$routeProvider.when("/Objects", {
templateUrl: "objects.html",
controller: ObjectCtrl
});
And this works as expected, pulling in the correct partial. Note that I am not Rendering partials through ASP.NET MVC Controllers in this case. Instead I have objects.html lying around as plain html file and I am using the hash in the href so the ASP.NET MVC Routing does not kick in.
Routeprovide should be setup like below.
$routeProvider.when('/Workflow/WorkflowTransition', {
templateUrl: "WorkflowTransition.html",
controller: WorkflowTransitionCtrl
});
and URL should be enter in below formate.
http://localhost/#/Workflow/WorkflowTransition ?
I'm reading Pro ASP.NET MVC 3.0 from Appress.Chapter 11 is about URLs routing system.
In passing extra variables from outgoing section is explained about getting segment values from url
for example for bellowing routing :
routes.MapRoute("MyRoute", "{controller}/{action}/{color}/{page}");
if a user is currently at the URL /Catalog/List/Purple/123, and we render a link as
follows:
#Html.ActionLink("Click me", "List", "Catalog", new {page=789}, null)
The routing system will match against the route and It will generate the
following HTML:
Click me
But when i use this code for the following example it generates:
Click me
i don't understand why?
Try moving that route mapping above the other route mappings in your global.asax file