Route(URL) error for each controller - asp.net-mvc

I am new to ASP.Net MVC 4. I have 2 controllers
Customer
Supplier
each controller have a menu CustomerSetup and SupplierSetup.
The first visited menu is ok, e.g when i click on CustomerSetup Menu, url looks like localhost:1496/Customer/Index its ok, now when i click on Supplier Menu, url looks like localhost:1496/Customer/Supplier/Index instead of localhost:1496/Supplier/Index and below error is shown..
Server Error in '/' Application.
The resource cannot be found.

The problem is probably because you're using hand-coded URL to point to your Supplier.Index action. You better use the Url.Action() method instead:
Supplier Menu
See Documentation

I think using html helper can help:
#Html.ActionLink("Menu name(display to user)","Action_name","Controller_name")
This will take you to your desired controller action.
eg:
#Html.ActionLink("Home","Index","Home")

Related

Creating links using root url within Razor view

So I'm within a Razor view in an Asp.net Core app. I need to set links within the page. The problem is that this view belongs to a ProductController and the result of the link below is https://localhost:44390/product/test
Name
The links need to be like this: https://localhost:44390/test/ and not https://localhost:44390/product/test Which is the best way to achieve that?
So writing the link this way produces the output I want to:
Name

how to use resolve url in mvc controller

Can any body help me to resolve url (route url) in MVC
when i enter into the system it redirects me to Dashboard it works perfectly.
I have user defined menu whose routing value stored in database as follows
suppose i entered into the system it will redirect me to Dashboard page
suppose i want to redirect to usermaster page then url should be as follows
http://localhost:6782/Home/Index
but when i try to redirect to home index page the url should look like this
http://localhost:6782/Dashboard/~/Voucher/Create
i want to remove Dashboard/~
how plz let me know
If you're using IIS URL Rewriting within your MVC application, e.g. internally treating http://yoursubdomain.example.com/MyController/MyAction as http://hosted.example.com/yoursubdomain/MyController/MyAction, Url.Content() will generate a correct subdomain-relative link. ResolveUrl() will generate an incorrect link in this situation.
Source:Should I use Url.Content() or ResolveUrl() in my MVC views?
You should consider about #Url.Content("~\Action\").
You should not store the URL in the database as the URL may change according to the routing rules.
Try to store the controller name and action name in the database and in your code that builds the menu use #Url.Action and pass the controller and action retrieved from the database

How to properly encode links to external URL in MVC Razor

This view suppose to show a list of hyperlinks, each pointing to an external URL. The goal is for the user to click one of these links and have their browser open a new tab with the selected URL.
Currently I have the following markup:
#Html.ActionLink("SomeSite", "http://subdomain.mydomain.com/SomeSite")
This markup produces:
http://localhost:58980/AccessInstance/http%3a/subdomain.mydomain.com/SomeSite
instead of :
http://subdomain.mydomain.com/SomeSite
What can I change in my markup to make this work as I expect?
You don't need to use #Html.ActionLink for that. Just use a plain A tag:
SomeSite
Html.ActionLink is specifically for generating links to actions defined in MVC controllers, in the same app. Since you're linking to an absolute URL, you don't need any of the functionality that Html.ActionLink provides.
Two ways :
1. update the database column with full link:
eg SQL:
update ProductTable set ProductLink='http://www.example.com/Product/Mobiles' where ID=123
In asp mvc view
View
2. Hardcode the http part and list from model
View
Hope helps someone.
While a ViewBag is overused and not the best choice most of the time this is something that I had done when inheriting someone else's mvc app to do a quick fix for a URL that I needed to redirect to with a specific dynamically changing querystring parameter
<a target="_parent" href="http://localhost:56332/services/#ViewBag.factory">View Service</a>
Using .NET Core 6
This seems to be the most correct answer:
Link
This will generate the following result:
As you can see at the bottom left corner of the window before clicking the link, the URL address was rendered as it is (NOTE: The cursor was recorded out of place for some reason, that's a ShareX problem, ignore it).
Than link will be directly saved as a nvarchar(750) type (probably any character like type will do the work). No changes to the original link were made before saving it or on reading:
You need to take into account your RouteConfiguration.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}"
because you are specifying the action link as the entire link that you want to redirect.
I would recommend that you use the #rossipedia answer because you can make tricky things like putting a span inside the link
Here to display link that are clickable in index page
<td>
#Html.ActionLink(item.FileName, "../Uploads/Catalogue/"+item.FileName)
</td>

How to implement a search page which shows results on the same page?

I'm using ASP.NET MVC 2 for the first time on a project at work and am feeling like a bit of a noob.
I have a page with a customer search control/partial view. The control is a textbox and a button. You enter a customer id into the textbox and hit search. The page then "refreshes" and shows the customer details on the same page. In other words, the customer details appear below the customer search control.
This is so that if the customer isn't the right one, the user can search again without hitting back in the browser. Or, perhaps they mistyped the customer id and need to try again.
I want the URL to look like this:
/Customer/Search/1
Obviously, this follows the default route in the project.
Now, if I type the URL above directly into my browser, it works fine. However, when I then use the search control on that page to search for say customer 2, the page refreshes with the correct customer details but the URL does not change! It stays as
/Customer/Search/1
When I want it to be
/Customer/Search/2
How can I get it to change to the correct URL?
I am only using the default route in Global.asax.
My Search method looks like this:
<AcceptVerbs(HttpVerbs.Get)> _
Function Search(ByVal id As String) As ActionResult
Dim customer As Customer = New CustomerRepository().GetById(id)
Return View("SearchResult", customer)
End Function
A good place to start might be NerdDinner if you havn't already.
In the mean time though The approach I'd use is to have a page that has my search box on it.
Then I'd have a <div> that I name "SearchResults". This will ultimately hold my results to the search.
I then have a PartialView which takes a model that has all the search results and renders them.
So when I click the button I do a call out to a jQuery action that takes the search parameter, performs the search and then returns my PartialView as rendered HTML.
Back in the client side I take that rendered HTML and replace the contents of my div with the HTML.
The keywords to google, or SO, are RenderPartial. This is back end code to render a partial view and give you html.
Also jQuery postbacks so that you can call an action in your controller.
use RedirectToRoute action result
link

Html.Action link and Html.RouteLink

Even If the url is same can i go to different action using Html.RouteLink and Action Link.Like when i click on news link i will go to news details.The url of this page is http://localhost:1390/en-US/latestnews/125.Now if i select the ddl of language in the site header in this pagei need to
go to the home page of the site.The ddl (on change) will take the same url but this time it needs to go to action in the sane controller.
The URL will direct to the controller/action on the first routing entry that it matches regardless of how you generate it. I'm not sure exactly what you are trying to accomplish, but I suspect that what you need to do is use javascript to direct to a different url, perhaps generated with Html.RouteLink instead of Html.ActionLink based on the value of the drop down list when it's selected. If I'm misunderstanding what you are trying to do, please clarify.

Resources