ASP.NET MVC routing aspx file - c#

So I'm writing an ASP.NET MVC app and I have a little bit problem with routing aspx file - in general making this work.
Let's say I have a razor page and I want to, for example open specific row from database and show it, it's very simple and I just write in index.cshtml:
#Url.Action("Details", new { id = item.DB_Id })
And details page opens and I can see specific informations of this row in database
Code of routing:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
But when I want do the same but instead of opening details.cshtml file I'd like to do it with details.aspx (Web Form) appears a problem. Is controller has to be different, is code of routing has to be different? Or is it basically possible? And ideas or hints?

Url.Action helper doesn't create URL's to Web Forms pages because they aren't Actions. You'll need to do something like
#Url.Content("~/somefolder/Details.aspx?id=" + item.DB_Id)
Url.Content is meant for creating URL's to static files, but it also works well with Web Forms.
You could create your own helper that handles parameters more cleanly. I don't have time to do the implementation right now, but you could create something like:
#Url.WebFormsPage("~/somefolder/Details.aspx", new { id = item.DB_Id })
The helper could use reflection to generate the appropriate query string and append it to the URL.

Related

How can you make call a page without showing parameters on the URL?

I am trying to modify a project so that the routes, and the parameters do not show in the URL, when you access them through an <a> element.
This is the code on the View:
#Html.ActionLink("Edit", "EditSchedule", new { id = item.OID, iorDapa = item.IOR_HORARIO }) |
#Html.ActionLink("Delete", "DeleteSchedule", new { id = item.OID, iorDapa = item.IOR_HORARIO })
I've tried to change the Action Link for a RouteLink, by calling this
routes.MapRoute(
name: "EditSchedule",
url: "{controller}/{action}/{oid}_{iorDapa}",
defaults: new { controller = "Users", action = "Index", oid = UrlParameter.Optional, iorDapa = UrlParameter.Optional }
);
However, that didn't work, given that it keeps making a link element with an established URL, so I assume that something else is needed, perhaps some method I don't know to make a call to the api?
And... From here on I tried many things, but none seem to work.
From other projects I worked with (JS), I remember that the router called an API, which in turn called a controller file, and that controller did the logic to then call the view.
However, with this C# project I cannot get the program to call the WebApiConfig file, or the RouteConfig without actually showing the parameters on the URL.
I'm sorry If I have explained myself a tad bad, I'm a bit lost in this!
From ActionLink data will be send using GET method and call data will be visible in the browser address bar. To hide what you are sending, use POST method. For example post your data by submitting form or placing AJAX call to the server with POST data

ActionLink and relative path

I can't find out how to solve this. I have two URLs. These are /my-url-1 and /my-url-2. Both going to different views.
The thing is that I have an ActionLink on /my-url-1's view which should make /my-url-2 and go to that view.
The problem is that ActionLink makes /my-url-1/my-url-2 as the URL and not just /my-url-2.
I was searching two days about how to fix it but couldn't find anything.
PD: I'm not using Areas so please don't tell me that I just should put the "area" parameter as a "".
These are two urls which goes to different controllers and different actions.
View which has the ActionLink (URL:/my-url-1) :
<div class="btn-index-container">
#Html.ActionLink("Url 2", "MyAction", "MyController")
</div>
This ActionLink should render:
Url 2
But it's rendering:
Url 2
where /my-url-1 is my current URL
Route Config
routes.MapRoute(
name: "route1",
url: "my-url-2", //without parameters
defaults: new { controller = "MyController", action = "MyAction" },
);
routes.MapRoute(
name: "route2",
url: "my-url-1", //without parameters too
defaults: new { controller = "MyController2", action = "MyAction2" }
);
So, when I go to localhost:port/my-url-1 it loads MyAction2 which renders a view. This view has inside an ActionLink(described above) which should render a /my-url-2 link.
Well, I've worked inside the MVC framework and I could told you about how Url.RouteUrl or Html.RouteLink works. At the end, the method which create the URL is GetVirtualPathForArea (this method is called before UrlUtil.GenerateClientUrl, which receive the VirtualPathData.cs created by GetVirtualPathForArea, as a parameter) from System.Web.Routing.RouteCollection.cs.
Here I left a link to the MVC source code:
https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Web.Mvc
I found that, my Request.ApplicationPath was changing when I loaded /my-url-1. It was crazy because the application path was /.
At last, the problem was that the /my-url-1 was pointing to a virtual directory created on the IIS some time ago by error.
To know where your IIS configuration file is, please follow the link below:
remove virtual directory in IIS Express created in error
The solution was remove the .vs directory (which contains the config .vs\config\applicationhost.config) and rebuild
I think most of the Helpers that render URLs works more or less in the same way, so I hope it'll useful for all of you!
In your case, maybe no its necesary, just pass the parameters with null values, E.G.
#Html.ActionLink("Español", null, null, new { Lng = "es" }, null)
In this way, the parameters change, and the view is relative, depending on where you are.

rewriting urls for simple html pages in asp.net MVC app

I have asp.net mvc application.
And I have html page. called lp.html.
i want to rewrite page name url from lp.html to something else, lets say appndomainname.com/location
how can I do this, because in this case I am dealing with simple html pages. No controller or controller method.
So code like this won't be needed
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
any help will be appreciated, thanks

vs 2013 mvc routing not working after change to cshtml view

This is a very strange problem. I am new to the mvc world coming from web forms and i am trying to understand its concepts. Using the MVC template in vs 2013 (premium), I have built a project. In order to see how things work:
I create a new controller 'IndexController' and put it in the folder .../Controllers/IndexController.cs
I create a new View for this controller 'Index.cshtml' and put it in the corresponding folder: .../Views/Index/[#] Index.cshtml
Then I change the routing so that the default routing will now point to this IndexController and not to the default HomeController
Here is my routing table:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Index", action = "GetIndexPage", id = UrlParameter.Optional }
);
}
}
You can see that I am using 'GetIndexPage' as the default action instead of 'Index' (I'm playing around and see how it works)
Whenerver I make a change in the Index.cshtml (say I add a simple markup and hit 'Run' I always receive the error message
Server Error in '/' Application. The resource cannot be found.
Looking at the address bar I see that the browser is looking for the resource 'localhost:xxx/Index/Index' instead of 'localhost:xxx/Index/GetIndexPage'. To solve this problem, I go in the IndexController and put a breakpoint inthe line
return View(...);
Now I hit 'Run', after stopping at the breakpoint, every thing works perfectly. So it is not a problem of routing since the page is displayed after this breakpoint trick. Visual Studio seems to mess up with the deployment to the IIS Express I am using after I have made a change to the cshtml view. The problem does not occur when I make a change in the code behind of the controller. I don't know where to look at...I have spent this whole night trying to find a solution in Google and stackoverflow...I don't want to reinstall the whole visual studio. Any help, any hint to a certain direction will be greatly appreciated.
After a lot of trials I discover that mvc framework wants the action and the view (that this action returns) must have the same name. In my case this is what has worked:
route configuration:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Index", action = "IndexPage", id = UrlParameter.Optional }
);
IndexController:
public class IndexController : Controller
{
public ActionResult IndexPage()
{
return View();
}
}
You can see that route action has same name with method of controller: IndexPage
And finally create a view with name IndexPage.cshtml
With this configuration when I make a change in the cshtml file it is immediately reflected in the browser and there is not the error reported above.
I just want to have a confirmation if this is indeed the way things must be set up with the mvc approach. (thanks with your help)

How to ignore all characters after "controller/action" in an ASP.NET MVC route?

I would like my ASP.NET MVC4 application to only serve the base HTML markup for a specific page, and after that I'm processing everything else on client-side with knockout.js/history.js/AJAX, including the initial page load.
So when someone refers to URL http://example.com/products/list/food/fruits, the MVC router should simply ignore everything what is behind "products/list" and route the request to ProductsController and List action. Then on client-side I will handle the rest and load the requested data accordingly.
I was playing with the route definitions, I tried to completely skip the "products/list" route, I also tried to add a "products/list/*" route, but didn't have success yet.
You can use an asterisk as part of the last variable in a route. For example, when configuring your routes:
routes.MapRoute(
"ProductRoute",
"products/list/{*otherArgs}",
new { controller = "Products", action = "List" });
You can learn more in MSDN's Documentation on routing under the section "Handling a Variable Number of Segments in a URL Pattern"
You will need to create your own route.
Something like this should do the trick:
routes.MapRoute("Products", "Products/{List}",
new {controller = "Products", action = "List"}
);
Note: I´m not sure if the other parameters are required in the route.

Categories