Using Areas in ASP.NET 5 - asp.net

I have an ASP.NET vNext (5) project. I am trying to add two areas to the project. My question is, how do I register areas in vNext? The System.Web.Mvc namespace is gone, which is where AreaRegistrationContext was located. I started looking in the MVC source code on GitHub. I found the Area attribute. However, I'm not sure how to utilize it now.
Can someone please explain to me (or provide a link) of how to use Areas in ASP.NET vNext?
Thank you!

In vNext you register and configure the services you are going to use in Startup.cs. Area routes are added like normal routes. There is a sample here: https://github.com/aspnet/Mvc/blob/a420af67b72e470b9481d6b2eca29f7c7c2254d2/samples/MvcSample.Web/Startup.cs
You could add an MVC route for an area like this:
app.UseMvc(routes =>
{
routes.MapRoute("areaRoute", "{area:exists}/{controller}/{action}");
});
Or you could use a route attribute like this: [Route("[area]/Home")]
The [Area] attribute decorates controllers included in the area. It takes only one parameter, the name of the area. Here's an example: https://github.com/aspnet/Mvc/blob/a420af67b72e470b9481d6b2eca29f7c7c2254d2/samples/MvcSample.Web/Areas/Travel/Controllers/HomeController.cs
[Area("Travel")]
public class HomeController : Controller
{ //... }

Related

How to pass an object from one page component to another page component in a .NET Blazor app?

I have a .NET Blazor Server app and need to pass an object from one component to another. Both components are pages, meaning that they have #page directives with routes. I know how to use cascading values to pass a parameter between regular Blazor components, but this does not work with page components. I also know how to pass a parameter within an endpoint route. However, instead of a string or int I want to pass an object with multiple properties and am unsure how to best accomplish this.
Is it possible to pass an object as an endpoint route parameter? If not, what is a good way to accomplish this within a Razor components context?
Using dependency injection would likely solve this issue for you.
Example:
Create a class called "ApplicationService"
Create an interface in that class called "IApplicationService"
You could have something like this
public interface IApplicationService
{
public Task MsgBox(string value);
}
In the ApplicationService class inside the "ApplicationService.cs" file, go ahead and implement the interface member above.
You could have something like this:
public async Task MsgBox(string value)
{
await _JSRuntime.InvokeAsync<string>("alert", value);
}
In the program.cs class, you need to now register that "service" we just created.
You could have something like this
builder.Services.AddTransient<IApplicationService, ApplicationService>();
builder.Services.AddScoped<ApplicationService>();
In your _Imports.razor you can inject the class so that the pages have access to it:
#inject ApplicationService MainAppService;
Now in your razor components you should be able to do something like this:
await MainAppService.MsgBox("This is a message box");
This works in my WASM blazor app, hope it sheds some light on the server side of things 🚀
Use a DI service. Create a class to hold your object. Add it as a Scoped Service in Program. Use it in any component (pages are just components with a page attribute) though #inject.

Xamarin Forms Prism Naming convention with subfolders

Is there a standered naming convention when making folders in a prism project ?
This works
ViewModals:
HelloWorldPageViewModel
View:
HelloWorldPage
App:
Container.RegisterTypeForNavigation<Views.HelloWorldPage >();
But for some reason , this does not work
I added the folling folders Login > Template >
ViewModals:
Login.Template.HelloWorldPageViewModel
View:
Login.Template.HelloWorldPage
App:
Container.RegisterTypeForNavigation<Views.Login.Template.HelloWorldPage >();
You have three options:
Change the naming conventions using the ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver. You can see an example in this blog post: http://brianlagunas.com/getting-started-prisms-new-viewmodellocator/
Or you can simply register your VM directly with ViewModelLocationProvider.Register<View, ViewModel>();
If you are using Xamarin.Forms simply provide the VM in the Container.RegisterTypeForNavigation<View, ViewModel>(); method
To the best of my knowledge Prism checks namepaces of ViewModels and Views.
So if your have a view it has to be under Views.Something , and if you want to have a viewmodel for it should be "ViewModels.SomethingViewModel"

What is an area parameter in Html.Action class?

I am studying ASP.NET MVC4. I don't quite understand what the area parameter is in the code below.
<section id="myWorkingSection">
#Html.Action("myActionName", "myController", new { area = "Widgets", workingSection = "myWorkingSection" })
</section>
myActionName takes workingSection as parameter but it does not take area as parameter. My guess is that since Widgets is the name of the folder, is it telling that the controller myController is located in the Widgets folder?
Thank you.
It is a route parameter used to specify the area within MVC.
In your case the area is Widgets.
Walkthrough: Organizing an ASP.NET MVC Application using Areas

How to get the current view name in asp.net MVC 3?

How can I get the current view name regarding to current URL, in asp.net MVC 3 using Razor engine?
No idea why you would need to get the current view name but you could use the VirtualPath property inside a view. Normally it's more useful to know the current action or controller. But anyway, here's how to get the current view name:
#VirtualPath
and if you wanted to get only the filename:
#Path.GetFileName(Server.MapPath(VirtualPath))
and without the extension:
#Path.GetFileNameWithoutExtension(Server.MapPath(VirtualPath))
I've also tested this code, and I could do something with it.
But, I'm not sure if is this a good solution or not.
For example, I need to detect the Contacts view located in Home directory. So I wrote:
if (#Request.RawUrl == "/Home/Contacts")
{
// do something
}
You can get it from RequestContext.RouteData
specifically, its Values collection contains "controller" and "action" keys
i.e.
RequestContext.RouteData.Values["controller"]
RequestContext.RouteData.Values["action"]
ASP.NET Core's equivalent:
#ViewContext.ExecutingFilePath
Output is like this:
/Views/Shared/String.cshtml
The rendering of a view may involve one or more files (e.g. _ViewStart, Layouts etc).
This property contains the path of the file currently being rendered.
ViewContext.ExecutingFilePath Property

How to use areas with controllers from a different assembly?

I'm starting a new ASP.NET MVC project, and I decided to put my controllers in a different assembly. Evertyhing works fine, but I have hit a problem: I created a new area in my MVC Project, called Administration. I have an AdminController Class in my seperate assembly which is supposed to return views from my Admin area, but everytime it tries to return a view,
it looks for it in the wrong place (~/Admin/SomeView.cshtml Instead of ~/Administration/Admin/SomeView.cshtml)
How can I tell the controller to look for views in the wanted area?
Please take a look into this article. And also you problem was answered here.
Basically you will need to extend MvcViewEngine, to tell MVC to look for your Views in the different from standatd pathes:
public class YourMegaViewEngine : WebFormViewEngine
{
public YourMegaViewEngine ()
{
ViewLocationFormats = new string[]
{
"~/Views/Administration/{1}/{0}.cshtml" //I may be wrong for you case, but this is the place to puth you path
};
}
}

Resources