Taghelpers get overwritten by ViewComponent - asp.net

I have ASP.NET Core application (fka ASP.NET 5, ASP.NET MVC 6). The layout page has the following relevant links:
<li><a asp-controller="order">Orders</a></li>
<li><a asp-controller="customer">Customers</a></li>
<li><a asp-controller="whatever">Something Else</a></li>
and then (via ViewComponent)
<li><a asp-controller="account">Login</a></li>
Login.cshtml has the following:
<form asp-controller="Account" asp-action="Login" method="post" role="form">
...
</form>
or <form action="/Account/Login">...</form> - doesn't make any difference.
The problem is that once I click on Login and get into login screen, the top level links (Orders, Customers, etc.) all turn into "/Account/Login"! Obviously, if I put old-fashioned href, they remain unchanged. But the most puzzling part is that if I specify Action explicitly -
<li><a asp-controller="order" asp-action="Index">Orders</a></li>
Everything works correctly! Is it a bug that I stumbled upon, or is it by design that I don't understand?

The Anchor tag helper defaults to empty when it's unable to find the specified route, so it probably just defaulted to the current page.
<a asp-controller="SomeUnknownController">Link!</a>
Will produce
Link!
Since we don't know what's going on in your Account/Login action we cannot say for sure this is a bug in the anchor TagHelper. But something is messing with the routing somewhere.

Related

how to check li click events to code behind using asp.net

I have list li,I want to know which option in clicked to code behind.How can i possible
<ul class="mega-select__sort">
<li class="filter-wrap"><a href="#" id="loc" class="mega-select__filter filter--active" runat="server" data-filter='location'>Location</a></li>
<li class="filter-wrap"><a href="#" id="cine" class="mega-select__filter" runat="server" data-filter='cinema'>Cinema</a></li>
</ul>
there are two options one is location and another one is cinema.I want to know which option was clicked to code behind..
You cannot do this. After a page has finished processing and has been sent to a browser as the HTTP response, server can do nothing with this page.
You need to use POST form to inform the server about your choice. Or you can use JavaScript and AJAX, if you want it to be done without form submitting and reloading.
Use repeater control or Datalist control to list the <li> tag. Inside <li> take linkbutton. then use itemcommand method to handle it

New to ASP.NET and i have no idea why this #RenderPage isn't working

I've been tasked with updating my company's website and one of the first things I've noticed that they have they drop down menu coded on each page (roughly 60) So the first thing I would like to do is create the menu once, and call it from each page, so that I don't need to create the same menu 60 times. I know in php I could just throw in an #include and everything would be fine, but the company has forbidden me from using php and I have to work with their aspx. After doing a little research I found that asp.net has the #RenderPage statement which looked like it would work, I tried it out in Visual Studio Express on a simple test.vbhtml program and everything worked fine. I then went to the company's Default.aspx page and tried it and instead of rendering my menu it just displayed the #RenderPage("_Menu.vbhtml") line and not the menu file. Thinking I messed something up, I commented out the entire page and left a simple program at the bottom
<html>
<head>
<title>Main Page</title>
</head>
<body>
#RenderPage("_test.vbhtml")
<h1>Index Page Content</h1>
<p>This is the content of the main page.</p>
</body>
</html>
and I still get the same results, It doesn't Render the page and just displays my command as if I had wrapped it in a p tag. What am I doing wrong? Is .aspx incompatible with #RenderPage? If so how would you recommend I go about bringing in a menu from an outside file
Go to Views > Shared and see if there is an _Layout.vbhtml, that is kind of your master page, if the menu is located in that then every page which uses the layout should display the menu.
Many MVC apps use the bootstrap theme out of the box, and the _Layout.vbhtml contains:
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>#Html.ActionLink("Exceptions", "Index", "Exceptions")</li>
<li>#Html.ActionLink("Output File","Index", "MasterFile")</li>
<li>#Html.ActionLink("Reports","Index","Reports")</li>
</ul>
<p class="nav navbar-text navbar-right">Hello, #User.Identity.Name!</p>
</div>
That sort of thing to render the menu across every page which uses the layout.
I believe you are making a big confusion between ASP, ASP.NET MVC and Razor view engine.
Simply put: #RenderPage won't work in .aspx. The page itself has to be a vbhtml file as a Razor view.
Or you may use <% RenderPage(...) %> with correct tagging.

Hide Menu option based on Role Visual Studio 2013 asp.net web site

When creating a standard asp.net web site with vs2013 I notice that no site map is used and I also note that the menu created is a new nav bar class.
You get this as a default:
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a runat="server" href="~/">Home</a></li>
<li><a runat="server" href="~/About">About</a></li>
<li><a runat="server" href="~/Contact">Contact</a></li>
<li><a runat="server" href="~/Upload">Upload Files</a></li>
<li><a runat="server" href="~/Portal/UploadCust">Customer Portal</a></li>
</ul>
So, no site map is used here. And the menu system in asp.net NOW does not use the site.map. What is the suggested approach NOW THAT NO SITEMAP is used in these templates to hide a given menu based on role?
Is the suggestion to create a sitemap by hand? and then simply enable trimming as was done in the past? I have role permissions working fine (sql providers) but I am perplexed why this new menu system does not support nor have anything to do with site maps? I am simply looking to hide menu options based on role membership but am at a loss as to why the default menu systems don't use sitemap anymore? Right now users just get a 404 error.
So is some suggested way to hide menu options in above or is it still best to adopt a site map and enable trimming? This being the case then must I drop use of the above new nav bar class?
edit:my current solution is to add a id like this
"<a id="PortalLink" runat="server" href="~/Portal/UploadCust">Customer Portal</a>
And then in the web site on-load in VB, I use this:
If Roles.IsUserInRole("Portal") Then
Me.PortalLink.Visible = True
Else
Me.PortalLink.Visible = False
End If

jquery Mobile aand Asp.net Button click event not firing and redirecting to back url

I have follwing code
<div class="main" data-role="content">
<div class="choice_list menuwrapper" >
<ul data-role="listview" data-inset="true">
<li><a href="CreateUser.aspx" data-transition="slidedown" >
Create User
</a></li>
<li><a href="WebForm1.aspx" data-transition="slidedown">
Manage User
</a></li>
<li><a href="Default.aspx" data-transition="slidedown">
Upload File
</a></li>
</ul>
</div>
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
</div>`
but when i try to click any button on createuser or other page the button_click event is not firing in asp.net and the page redirects to default.aspx,
I found when gooogled that using
$(document).bind("mobileinit", function () {
// jQuery Mobile's Ajax navigation does not work in all cases (e.g.,
// when navigating from a mobile to a non-mobile page), especially when going back, hence disabling it.
$.mobile.ajaxEnabled = false;
$.mobile.ajaxLinksEnabled = false;
});
it will solve, but no luck in my case,
Could somebosy please help me.
Thanks
I had a similar issue with a rather large mobile project. We also ran into other bugs when using .NET masterpages with jQuery Mobile. We ended up not using a masterpage in the end because there were just too many issues.
Try adding data-ajax="false" in all of your links. That should be what the code you posted is doing, but it might not be working depending on the version of JQM you're using.

Getting errors on redirect only from certain pages

I have a menu in my Site.Master file whose code is as follows;
<nav>
<ul id="css3menu1" class="topmenu">
<li class="topfirst"><span>Home</span>
<ul>
<li>Employee Login</li>
<li>Customer/Distributor Login</li>
</ul>
</li>
<li class="topmenu"><span>Products</span>
<ul>
<li>Page1</li>
<li>Page2</li>
<li>Page3</li>
</ul>
</li>
<li class="topmenu"><span>Investor Info</span>
<ul>
<li>Quarterly Filings</li>
<li>Press Releases</li>
<li>Investor Updates</li>
<li>Company Presentations</li>
<li>Management Team</li>
</ul>
</li>
</ul>
</nav>
When I go to the sites login page BUT do not login and try to go to any of the links in the menu I get a 404 error page which says I'm trying to redirect to /Account/Page1.aspx.
Why is the "Account/" getting inserted into that path when the path from the Site.Master is clearly only Page1.aspx. I've tried changing the href to ~/ and ../ infront of the page name but that produced different types of errors ALTHOUGH it did properly redirect ONLY when in the login page (which is in the Account folder)
When you access Login.aspx page It will in 'Account' Folder directory. When you try to access other links in menu, it will first check in current 'Account' directory if page is exist it will display page otherwise it throws error.
I suggest you require dynamic menu for this. when You access 'Account' directory page it will change the menu as per the 'Account' directory and link.
You can use literal control and Literal1.Text = 'HTML Menu Code'
Better practice to use the tilde symbol instead of a relative path but with this you will need to use the
runat="server"
in the tag.
what were the other errors you got with that approach?
edit
<li><a runat="server" href="~/Account/Login.aspx">Employee Login</a></li>
It was far easier, and much more practical solution to use Page.ResolveURL("~/Account/Login.aspx"). I used Page.ResolveURL for all the links in the menu and now it works seamlessly

Resources