How to remove Admin from Request url in MVC - asp.net-mvc

I am using below code to form url :
PricingLink = Url.Action("PricingRequestSummary", "GlobalPricing", new { id = pricingId }, this.Request.Url.Scheme)
Gives me result as :
http://localhost:56287/Admin/GlobalPricing/PricingRequestSummary/4
I want only (Admin i dont want)
http://localhost:56287/GlobalPricing/PricingRequestSummary/4
How to achieve that ?
Thanks

I am assuming you are executing the Url.Action helper method from a view under the Admin area. By default it will use the current area name when building the urls. But you can override this by explicitly passing an empty string as area name.
<a href="Url.Action("PricingRequestSummary", "GlobalPricing",
new { id = pricingId, area = string.Empty }, this.Request.Url.Scheme)">Test</a>

Related

ASP.Net Core MVC Routing Issue

I am working on a project (Developed by using .Net Core), I have set some routes and one of them is not working e.g.
1) routes.MapRoute("HRDetail", "H-R/{TName}/{MId}", new { controller = "ABC", action = "XYZ1" });
2) routes.MapRoute("CL", "{SName}/{CName}/{CId}", new { controller = "ABC", action = "XYZ2" });
I have written the code in the same sequence in Startup class, and my action methods are as follows.
public async Task<IActionResult> XYZ2(string SName, string CName, Int16 CId)
{//for route#2}
public async Task<IActionResult> XYZ1( string TName, Int64 MId)
{//for route#1}
Now I want to hit on XYZ1 by using route#1 and the link (to hit on XYZ1 is being created dynamically) is like this http://localhost:4321/H-R/UK/1234. But the problem is that when i click on this link, it always take me to XYZ2 method.
I didn't set any route on controller or action method level.
Is there any solution plz?
It seems, The route are getting confused. There are two ways you can fix this.
1) in your first route specify the regular expression which will say that first parameter will be a fixed string as H-R
2) in you second route specify the regular expression which will say that first parameter will never be H-R
1st
routes.MapRoute("HRDetail", "{ActionName}/{TName}/{MId}", new { controller = "ABC", action = "XYZ1" }, new{ActionName = "$your regularexpression to include only H-R$"});
OR
routes.MapRoute("CL", "{SName}/{CName}/{CId}", new { controller = "ABC", action = "XYZ2" }, new {SName = "$your regularexpression to exclude H-R$" });
PS: you need to put some efforts for regular expression

AspNet MVC render server side #Html.ActionLink

Im building a dynamic menu in my MVC project so in my controller I have the following:
// list accounts
menu.Add(new Model.Custom.Menu() { Active = true, Url = "dashboard.html", Icon = "mdi mdi-account-card-details", Items = null, Text = "List Accounts" });
Right now dashboard.html link is hardcoded but I required to be #Html.ActionLink so it renders the right path.
Any clue?
You can use the UrlHelper to call the Action method which will return the correct relative path to the action method.
var url=new UrlHelper(Request.RequestContext);
var newUrl = url.Action("Dashboard");
//The above will generate the path to Dashboard action method.
menu.Add(new Model.Custom.Menu() { Active = true, Url = newUrl});

MVC Model Binding Issue always NULL

I am trying to add a new MVC ActionLink that when pressed goes to an action and gets bound to the input parameter but it doesn’t seem to be working
I Have this:
#Html.ActionLink("OPTIN","Optin", "Admin", new {id = Model.Publications[i].ID})
And then the Optin method on the Admin controller:
public ActionResult OptIn(int? id)
{
if (id.HasValue)
{
var temp = id.Value;
}
I want the id to get mapped to the input parameter but it is always NULL and Model.Publications[i].ID is always 5. What am I doing wrong? Do I have to make ID part of a model?
The way that you have your ActionLink method set up is wrong. I'm GUESSING the override you're using is this one. However, you'll want to be using is string, string, string, object, object.
By the URL in the comment on your question, the ID that you're setting is the HTML attribute, not a part of the URL.
If you change your call to the following, it should give you what you need:
#Html.ActionLink("OPTIN","Optin", "Admin", new {id = Model.Publications[i].ID}, new { })

Generate hyperlink in ASP.NET MVC 2 controller?

I've been scouring the web for a way to do this.
I want to generate a hyperlink to an action from my controller and put it in a string. I need to be able to define the label and give it html attributes. I can get Url.Action(...) working but that method doesn't let me define the label on the link.
HtmlHelper.GenerateLink(...) looks promising but I can't find any concrete examples on how to use it.
The link should look something like this:
View
Add this property to your base controller:
protected HtmlHelper Html
{
get
{
var viewContext = new ViewContext( ControllerContext, new WebFormView( Request.CurrentExecutionFilePath ),
new ViewDataDictionary(), new TempDataDictionary(), Response.Output )
{
RouteData = ControllerContext.RouteData
};
return new HtmlHelper( viewContext, new ViewPage() );
}
}
and then call it from anywhere:
var link = Html.ActionLink( "Click Me", "action" );
try this
string str = string.Concat("View"
and then pass this in ViewData and call it in view
<%= str%>
there are a few ways to do this - here are 2:
Link name here
Html.ActionLink(article.Title,
"Login", // <-- Controller Name.
"Item", // <-- ActionMethod
new { id = "<arguments here" }, // <-- Route arguments.
null // <-- htmlArguments .. which are none. You need this value
// otherwise you call the WRONG method ...
// (refer to comments, below).
)
there are other overloads of each available
Perhaps a little more information on why you would want to do this would be a little more helpful. If you return a string that contains HTML it will by default be HTML encoded and rendered useless on the client. If you have a custom view where this will be rendered why not build the link there using #Html.ActionLink?
I guess I am trying to figure out the benefit of doing it in the controller rather than the view...

How can I pass the URL to the controller while routing ignores it?

Is there a way within asp.net MVC 2 whereby I can route a request and have a portion of the URL ignored and passed to the controller as a variable?
My needs state that I must store pages dynamically in a database, and they should be accessible by looking at the URL and reading the URL segments to find the relevant page. Effectively, I need a Site controller, to which the remaining portion of the URL will be passed.
Site-Controller/this/is/a/page
So this in case the site controller would pick up the /this/is/a/page 'string'
Is this possible?
Thanks!
Yes, use a wildcard route, like:
routes.MapRoute(
"SiteController", // Route name
"Site-Controller/{*url}", // URL with parameters
new { controller = "SiteController", action = "Index" }, // Parameter defaults
null // constraints
);
Then your action looks like:
public ActionResult Index(string url)
{
}
Create a wildcard Route in Global.asax which captures everything after the first segment of the url and passes it to your Action method:
routes.MapRoute("Page",
"Site-Controller/{*urlsegments}",
new {
controller = "Site-Controller",
action = "YourAction",
urlsegments = ""
});
Make sure your Action method accepts a 'urlsegments' parameter and you can work with it from there:
public ActionResult YourAction(string urlsegments)
{
// Do something with the segments here
}

Resources